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$..$/

In reply to Re: array of hashes by liverpole
in thread array of hashes by megaurav2002

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.