CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
multiple values per key in hash

 



artperl
Novice

Sep 17, 2015, 8:14 AM

Post #1 of 8 (2126 views)
multiple values per key in hash Can't Post

dear perl gurus,

need some consultation on what would be the most efficient method to code in perl.

say i have a file that contains:

Key1 - value1 - value2
Key2 - value3 - value4
Key3 - value8 - value20

how can i efficiently read this file into a hash for internal manipulation/checking then update back the file?


Laurent_R
Veteran / Moderator

Sep 17, 2015, 8:21 AM

Post #2 of 8 (2119 views)
Re: [artperl] multiple values per key in hash [In reply to] Can't Post

The best choice of data structure to be used really depends of what you need to do to process the data afterwards.

Assuming you just want to build a hash of arrays, it could be something as simple as this:


Code
my %hash; 
while (<$IN>) {
chomp;
my ($key, @vals) = split /[-\s]+/, $_;
$hash{$key} = [@vals];
}


But, again, another data structure might be more appropriate for what you want to do next with the data.

Update: fixed two small errors noticed by FishMonger in his post below. Thanks, FishMonger, for pointing out those mistakes.


(This post was edited by Laurent_R on Sep 17, 2015, 10:45 AM)


FishMonger
Veteran / Moderator

Sep 17, 2015, 8:50 AM

Post #3 of 8 (2116 views)
Re: [Laurent_R] multiple values per key in hash [In reply to] Can't Post

Why have \n in the character class? I think you meant to use \s instead of \n and you forgot < > around $IN


Laurent_R
Veteran / Moderator

Sep 17, 2015, 10:43 AM

Post #4 of 8 (2114 views)
Re: [FishMonger] multiple values per key in hash [In reply to] Can't Post

Yes, you are right, I of course meant \s instead of \n. And you're also right about the forgotten angle brackets around the file handle.

I'll correct mypost accordingly.


(This post was edited by Laurent_R on Sep 17, 2015, 10:45 AM)


artperl
Novice

Sep 18, 2015, 2:13 AM

Post #5 of 8 (2102 views)
Re: [Laurent_R] multiple values per key in hash [In reply to] Can't Post

dear laurent,
thanks for the inputs... let me try that... was thinking too complex when an easy solution like what you advised could work...
much appreciated.


bulrush
User

Sep 19, 2015, 6:00 AM

Post #6 of 8 (2086 views)
Re: [artperl] multiple values per key in hash [In reply to] Can't Post

You have to separate different parts of your data with a non-printing character that is guaranteed not to be part of your regular data, such as chr(1) or chr(9) (tab). I do this all the time and tab works in 99% of my programs.


Code
my $SEP=chr(9); 
# Store it in the hash.
my $stuff=$data1.$SEP.$data2;
my $key='1';
my $hash{$key}=$stuff;
# Retrieve from hash.
$t=$hash{$key};
my @a=split($SEP,$t);
for (my $i=0; $i<=$#a; $i++)
{
print "$a[$i]\n";
}

-----


FishMonger
Veteran / Moderator

Sep 19, 2015, 6:33 AM

Post #7 of 8 (2083 views)
Re: [bulrush] multiple values per key in hash [In reply to] Can't Post

That's a very messy and poor approach.

If you need to store multiple values, you should store them as an array. Concatenating the values to store them as a string which you then latter need to split into an array is silly.

Use the array approach already shown.


Laurent_R
Veteran / Moderator

Sep 20, 2015, 2:21 AM

Post #8 of 8 (2067 views)
Re: [FishMonger] multiple values per key in hash [In reply to] Can't Post


In Reply To
That's a very messy and poor approach.

If you need to store multiple values, you should store them as an array. Concatenating the values to store them as a string which you then latter need to split into an array is silly.

Use the array approach already shown.


Indeed, ++, I could not agree more. I should add that it doesn't scale well if there are more that two values in the input data and also that I don't understand why $key should be assigned to 1.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives