in reply to goto HACK
If you only have timestamps, you can just count them:
#!/usr/bin/env perl use strict; use warnings; use Data::Dumper; my @times = qw(1000 1000 1000 1010 1010 1010); my %data; foreach my $key (@times) { $data{$key}++; } print Dumper(\%data);
Results:
$ perl timehash1.pl $VAR1 = { '1000' => 3, '1010' => 3 };
On the other hand, if you have data associated with the timestamps, you can just push that data into an array associated with that specific timestamp inside the hash. Sounds complicated, but it isn't:
#!/usr/bin/env perl use strict; use warnings; use Data::Dumper; my @times = (1000 => 'hello',, 1000 => 'world', 1000 => 'foo', 1010 => 'bar', 1010 => 'baz', 1010 => ['hello world', 'hallo welt'], ); my %data; while(@times) { my ($key, $val) = (shift @times, shift @times); push @{$data{$key}}, $val; } print Dumper(\%data);
And here is the result:
$ perl timehash2.pl $VAR1 = { '1010' => [ 'bar', 'baz', [ 'hello world', 'hallo welt' ] ], '1000' => [ 'hello', 'world', 'foo' ] };
Edit: If you want to avoid temporary variables, you can also make the main loop faster but harder to read:
while(@times) { push @{$data{shift @times}}, shift @times; }
|
|---|