473,405 Members | 2,154 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Parsing a CSV file into a Hash

7
Hi,
Im a new to perl and i hope there are people here that can help me!

I have a realy large file like this.
1,12,T,
2,13,B,
3,14,T,
4,15,B,

I want to use the 2nd and 3th column and put it into a hash like

with the 2nd column being the keys and the 3th column being the values.

This is what i have but i doesn't work!

open (data, 'data.csv');
my $hash_data = <data>;
my @hash_data = split (/,/, $hash_data);
@hash_Key = $hash_Data[1];
@hash_Values = $hash_Data[2];
%hash_data =($hash_Key => $hash_Values);
print %hash_data;

I hope someone can help me!

Iris
Feb 28 '07 #1
3 13871
arne
315 Expert 100+
Hi,
Im a new to perl and i hope there are people here that can help me!

I have a realy large file like this.
1,12,T,
2,13,B,
3,14,T,
4,15,B,

I want to use the 2nd and 3th column and put it into a hash like

with the 2nd column being the keys and the 3th column being the values.

This is what i have but i doesn't work!

open (data, 'data.csv');
my $hash_data = <data>;
my @hash_data = split (/,/, $hash_data);
@hash_Key = $hash_Data[1];
@hash_Values = $hash_Data[2];
%hash_data =($hash_Key => $hash_Values);
print %hash_data;

I hope someone can help me!

Iris
You could try something like
Expand|Select|Wrap|Line Numbers
  1. open DATA, "data.csv";
  2.  
  3. my %hash;
  4.  
  5. while( <DATA> ) {
  6.  
  7.     @elements = split /,/, $_;
  8.  
  9.     if( $elements[1] ne '' ) {
  10.         $hash{ $elements[1] } = $elements[2];
  11.     }
  12. }
  13.  
Feb 28 '07 #2
Iris83
7
Thanks this works!
Feb 28 '07 #3
@Iris83

Just another possibility:

Expand|Select|Wrap|Line Numbers
  1. open(DATA, "<", "data.csv") or die $!;
  2.  
  3. my %hash;
  4.  
  5. %hash = ( %hash, (split(/,/, $_))[1,2] ) while ( <DATA> );
Greetz, Doc
Feb 28 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: _DS | last post by:
I'm currently using a switch with about 50 case statements in a stretch of code that's parsing XML attributes. Each case is a string. I'm told that switch statements will actually use hash tables...
1
by: sp | last post by:
i have an xml file (an rss file) <?xml version="1.0" ?> <rss version="2.0"> <channel> <title>CodeGuru.com</title> <link>http://www.codeguru.com/</link> <description>The number one developer...
5
by: EP | last post by:
This inquiry may either turn out to be about the suitability of the SHA-1 (160 bit digest) for file identification, the sha function in Python ... or about some error in my script. Any insight...
1
by: John Wright | last post by:
I want to create a file hash on my exe files and store the hash signature in a database so I can retrieve the value to compare the file hash to ensure I have an untampered file. How can this be...
14
by: mens libertina | last post by:
Disclaimer: In addition to reading the skimpy entries at php.net, I've searched this group and alt.php to no avail. Basically, I'm trying to use a template to send email reminders, but I can't...
2
by: joe2684 | last post by:
I have got a file containing info abt node id and labels, which looks like this -645 _1__46__1__46__1__46__145__45__RXN -435 _3__45__OXOACYL__45__ACP__45__REDUCT__45__RXN -1084 RXN__45__4262...
2
by: amittewarii | last post by:
Hello Everybody , I need help in parsing the given file , ################################################################# <sample_cfg : 00FF_F000> #f_name ...
4
by: subeen | last post by:
Hi, I am trying to parse a rss similar to the one found here (in example) http://www.feedforall.com/mediarss.htm <!-- Snipped for Brevity --> <item> <title>FeedForAll's Show Tunes and...
6
by: whiteideal | last post by:
hay, guys, i just want to create a svg file according to my data, but i don't know whether there is some opensouce lib to do such things. Can you tell me about it? Thanks.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.