LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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, 06: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, 06:26 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,800

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

Original Poster
Blog Entries: 3

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, 07:14 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,800

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
what is your code?
 
Old 03-04-2013, 07:33 AM   #5
kedarp
Member
 
Registered: Jul 2012
Distribution: Ubuntu
Posts: 198

Original Poster
Blog Entries: 3

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, 07: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, 07:41 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,800

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
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, 07:47 AM   #8
kedarp
Member
 
Registered: Jul 2012
Distribution: Ubuntu
Posts: 198

Original Poster
Blog Entries: 3

Rep: Reputation: 23
Thanks a lot...
 
Old 03-04-2013, 09:17 AM   #9
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

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


Reply



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 02:05 PM
Perl hash of hashes props666999 Programming 2 09-07-2006 04:43 AM
Perl hashes ShaqDiesel Programming 6 08-09-2006 02:54 AM
Nesting Hashes in Perl. faref Programming 2 06-07-2006 05:03 PM
weird behaviour with hashes in perl weird_guy Programming 0 06-22-2004 09:51 PM

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

All times are GMT -5. The time now is 01:58 AM.

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
Open Source Consulting | Domain Registration