LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-04-2013, 07:14 AM   #1
kedarp
Member
 
Registered: Jul 2012
Distribution: Ubuntu
Posts: 198
Blog Entries: 3

Rep: Reputation: 23
need help with Perl hashes!!!


Hello,,

Here is a Perl program that i am stuck at. Need help with it.

I have a file file.txt with records.

Quote:
abc 23759
pqr 23597
xyz 80385
Now I have to extract the records in a hash
so that I can look up the nos. using the names.

Code:
open FILEIN,"<file.txt";

while($line=<FILEIN>){
	
   split(' ',$line);
}
I don't know how to create a hash from this code, using split.

Regards.
 
Old 03-04-2013, 07:26 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 11,481

Rep: Reputation: 3452Reputation: 3452Reputation: 3452Reputation: 3452Reputation: 3452Reputation: 3452Reputation: 3452Reputation: 3452Reputation: 3452Reputation: 3452Reputation: 3452
just google, see example 10: http://perlmeme.org/howtos/perlfunc/split_function.html
 
Old 03-04-2013, 08:04 AM   #3
kedarp
Member
 
Registered: Jul 2012
Distribution: Ubuntu
Posts: 198
Blog Entries: 3

Original Poster
Rep: Reputation: 23
That is where th problem is.

When the program comes out of the while loop,
only the last record remains in the hash.

So, that gives me only the last record -

xyz 80385

in the hash.
 
Old 03-04-2013, 08:14 AM   #4
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 11,481

Rep: Reputation: 3452Reputation: 3452Reputation: 3452Reputation: 3452Reputation: 3452Reputation: 3452Reputation: 3452Reputation: 3452Reputation: 3452Reputation: 3452Reputation: 3452
what is your code?
 
Old 03-04-2013, 08:33 AM   #5
kedarp
Member
 
Registered: Jul 2012
Distribution: Ubuntu
Posts: 198
Blog Entries: 3

Original Poster
Rep: Reputation: 23
Code:
open FILEIN,"<file.txt";

while($line=<FILEIN>){
	
   %records = split(' ',$line);
}
This is the code, but this is failing.
What I want to do is store the below records which I read from
a file in a Hash.

Quote:
abc 23759
pqr 23597
xyz 80385
Regards.
 
Old 03-04-2013, 08:35 AM   #6
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 982
Blog Entries: 2

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
%elephant=();
open FILEIN,"<file.txt";

while($line=<FILEIN>){
   @q=split(/\s+/,$line);
   $elephant{$q[0]}=$q[1];
}

foreach (sort keys %elephant) {
    printf(""K=%s  V=%s\n", $_, $elephant{$_});
}
 
1 members found this post helpful.
Old 03-04-2013, 08:41 AM   #7
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 11,481

Rep: Reputation: 3452Reputation: 3452Reputation: 3452Reputation: 3452Reputation: 3452Reputation: 3452Reputation: 3452Reputation: 3452Reputation: 3452Reputation: 3452Reputation: 3452
that one is not failing, just you did not print out the content of your hash....
 
1 members found this post helpful.
Old 03-04-2013, 08:47 AM   #8
kedarp
Member
 
Registered: Jul 2012
Distribution: Ubuntu
Posts: 198
Blog Entries: 3

Original Poster
Rep: Reputation: 23
Thanks a lot...
 
Old 03-04-2013, 10:17 AM   #9
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Debian, Mint, Puppy, Raspbian
Posts: 3,433

Rep: Reputation: 203Reputation: 203Reputation: 203
or
Code:
while (<>) {
   my ($k, $v) = split;
   $record{$k} = $v;
}
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl: Searching through hashes Poetics Programming 4 02-27-2009 03:05 PM
Perl hash of hashes props666999 Programming 2 09-07-2006 05:43 AM
Perl hashes ShaqDiesel Programming 6 08-09-2006 03:54 AM
Nesting Hashes in Perl. faref Programming 2 06-07-2006 06:03 PM
weird behaviour with hashes in perl weird_guy Programming 0 06-22-2004 10:51 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:12 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration