On 23/04/2013 13:26, kavita kulkarni wrote:
> Can you help me in finding the most time effective way to retrieve the
> key-values from hash in the same sequence in which data is inserted into
> the hash.
But why would you want that?
You can push the keys in an array as well.
But you will often have a list of the keys already.
#!/usr/bin/perl
use strict;
use warnings;
use v5.10; # say
my ( @keys, %row, @rows );
while ( <DATA> ) {
chomp;
my ( $k, $v ) = split " ", $_, 2;
$row{ $k } = $v; # unordered
push @keys, $k; # ordered-1
push @rows, [ $k, $v ]; #ordered-2
}
say "";
for my $k ( keys %row ) {
say "$k: <$row{$k}>";
}
say "";
for my $k ( @keys ) {
say "$k: <$row{ $k }>";
}
say "";
for my $r ( @rows ) {
say sprintf "%s: %s", @$r;
}
say "";
__DATA__
foo-A 42 and a bit
bar-B 81 or less
baz-C 17 forever
xxx-D xxx
yyy-E yyy
--
Ruud