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