Beginner question: need help importing tab-delimited file as perl hash

Reply

Join Date: Apr 2005
Posts: 3
Reputation: Commander Salam is an unknown quantity at this point 
Solved Threads: 0
Commander Salam Commander Salam is offline Offline
Newbie Poster

Beginner question: need help importing tab-delimited file as perl hash

 
0
  #1
Apr 18th, 2005
Hi everyone,

I'm trying to:

(1) take an excel spreadsheet with two columns of data
(2) convert to tab-delimited text file
(3) import into perl as a hash

I've managed to import as an array:

open (LIST1, "list1.txt") || die "File not found\n";
while (<LIST1>) {push(@list1array, $_)};
close LIST1;

However, I can't figure out how to either import directly as a hash or convert the array to a hash. If someone could point me in the right direction, I'd be appreciative.

Thanks!
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: Beginner question: need help importing tab-delimited file as perl hash

 
0
  #2
Apr 19th, 2005
Originally Posted by Commander Salam
Hi everyone,

I'm trying to:

(1) take an excel spreadsheet with two columns of data
(2) convert to tab-delimited text file
(3) import into perl as a hash

I've managed to import as an array:

open (LIST1, "list1.txt") || die "File not found\n";
while (<LIST1>) {push(@list1array, $_)};
close LIST1;

However, I can't figure out how to either import directly as a hash or convert the array to a hash. If someone could point me in the right direction, I'd be appreciative.

Thanks!

I would do it something like this:
  1. open (LIST1, "list1.txt") || die "File not found\n";
  2. while (<LIST1>) {
  3. ($tmpvar1, $tmpvar2) = split(/\t/, $_);
  4. $hashname{$tmpvar1} = $tmpvar2;
  5. }
  6. close(LIST1);

Which will open the file, read in the data line by line, split the current line by the tab character (so that what is in the first column will be in tmpvar1, and what is in the second column will be in tmpvar2) then assign the hash in a scalar context. I've tested it with a tab delimited text file, and it works fine for me.... let me know how it works for you.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 3
Reputation: Commander Salam is an unknown quantity at this point 
Solved Threads: 0
Commander Salam Commander Salam is offline Offline
Newbie Poster

Re: Beginner question: need help importing tab-delimited file as perl hash

 
0
  #3
Apr 19th, 2005
Works great. Thanks for the assistance!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC