Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

array of hashes

by megaurav2002 (Monk)
on May 13, 2007 at 11:33 UTC ( #615160=perlquestion: print w/replies, xml ) Need Help??

megaurav2002 has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks, I want to create an array of hashes. And in each hash I am copying values of another array. This is what I am doing:
foreach my $element( @record_time){ $each_record{'time'} = $element; push @store_all_data, \%each_record; }
But when i dump store_all_data, its just storing the first value of @record_time. I think the problem is that the key is same everytime, though i am storing each hash in different index of the array. Any ideas ??? Thanks! UPDATE -> Oh actually i have just tried something: i wrote
push @store_all_data, {%each_record}
instead of push @store_all_data, \%each_record and it worked. Can anyone please explain me the difference between the two.. Cheers!

Replies are listed 'Best First'.
Re: array of hashes
by jettero (Monsignor) on May 13, 2007 at 11:42 UTC
    Almost there.
    my %h = (); foreach(@_) { $h{something} = $_; # changes the %h above every time push @something, \%h; # puts the %h into @something multiple times +... }

    my %h = (); foreach(@_) { $h{something} = $_; # changes the %h above every time push @something, {%h}; # copies the accumulated changes into @somet +hing }

    foreach( @_ ) { my %new_hash = (something => $_ ); # make a new hash push @something, \%new_hash; # push it onto the stack }

    foreach(@_) { push @something, { something => $_ }; # make a new hash and push i +t onto @something! Magic! }

    For all the details of all the nuances above, look at perldata and perlref. I re-read perlref twice a year or more. I frequently find something new.

    -Paul

Re: array of hashes
by liverpole (Monsignor) on May 13, 2007 at 13:48 UTC
    Hi megaurav2002,

    Were you trying to create your hash based on the actual time of day?

    You could use time in place of the string 'time', (which is only going to overwrite the previous hash value, as hash keys are unique):

    my %each_record = ( ); foreach my $element( @record_time){ my $time = time(); $each_record{$time} = $element; }

    But you're still more than likely to overwrite some of your keys, because time() only gives you a 1-second granularity.

    You could also use something like Time::HiRes, and keep track of the previous time to make sure you don't reuse any keys.  Here's a way you could do that (in a full program, including strict, warnings and some test data):

    use strict; use warnings; use Time::HiRes qw( gettimeofday ); use Data::Dumper; # Test data my @record_time = qw( 12 23 34 45 56 67 78 89 90 ); # Current time to the microsecond my ($sec, $usec) = gettimeofday; my $last_time = "$sec.$usec"; my %each_record = ( ); foreach my $element(@record_time) { my $this_time = $last_time; while ($this_time eq $last_time) { # Always true the first time ($sec, $usec) = gettimeofday; # Get new current time until $this_time = "$sec.$usec"; # it's different from last } $each_record{$this_time} = $element; # Unique key $last_time = $this_time; # Reset the last time } # Verify the results printf "Results => %s\n", Dumper(\%each_record);

    Regardless of how you design your algorithm, it's a good idea to use Data::Dumper liberally.  Put it anywhere in your code that you're curious what the current results are, and take it out only after you understand what's happening to the data structure.

    For example, in your original code, the following might help you see what the underlying data looks like, at the end of each loop:

    use Data::Dumper; my %each_record = ( ); my @store_all_data = ( ); foreach my $element (@record_time) { $each_record{'time'} = $element; printf "\$each_record{time} [%s]\n", Dumper(\$each_record{'time'}); printf "\$each_record hash [%s]\n", Dumper(\%each_record); push @store_all_data, \%each_record; printf "\@store_all_data [%s]\n", Dumper(\@store_all_data); }

    Update:  I see from your homenode that you're very new to Perl, but also up for the challenge.  With that great attitude, and combined with your C skills, I predict you'll become an excellent Perl programmer.  And no matter what stage you're at, you'll fit right in here at Perlmonks.


    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
      Thanks everyone for the help. Thanks liverpole for your wishes! With so many helpful people around, I am really enjoying this place :)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://615160]
Approved by jettero
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (8)
As of 2021-06-24 22:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)












    Results (133 votes). Check out past polls.

    Notices?