in reply to compare previous and present hash key values
The problem is that hashes do not preserve order, you need an array @result for that or you have to store the order of keys separately in an array @order.¹
that's one way to go pairwise:
DB<120> @order=1..5 => (1, 2, 3, 4, 5) DB<121> $last= $order[0] => 1 DB<122> for $cur (@order[1..$#order]) { print "$last,$cur\t"; $last =$cur; } => "" 1,2 2,3 3,4 4,5
For completeness, a shorter but trickier way, using reduce:
DB<126> use List::Util qw/reduce/ DB<127> reduce { print "$a,$b\n"; $b } @order 1,2 2,3 3,4 4,5
HTH! =)
Cheers Rolf
( addicted to the Perl Programming Language)
¹) e.g.
while ( my $rec = $data->fetchrow_hashref ) { push @{ $result{ $rec->{"ID"} } }, $rec->{"item"}; push @order, $rec->{ID}; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: compare previous and present hash key values
by undisputed (Initiate) on Jan 11, 2014 at 16:03 UTC | |
by LanX (Saint) on Jan 11, 2014 at 16:19 UTC |