Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to retrieve the keys from hash in the sequence in which data is inserted

4 views
Skip to first unread message

kavita kulkarni

unread,
Apr 23, 2013, 7:26:55 AM4/23/13
to begi...@perl.org
Hello All,

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.

Thanks in advance.

Regards,
Kavita

David Precious

unread,
Apr 23, 2013, 7:37:58 AM4/23/13
to begi...@perl.org
On Tue, 23 Apr 2013 16:56:55 +0530
kavita kulkarni <kavitah...@gmail.com> wrote:

> Hello All,
>
> 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.

Normal hash ordering is unpredictably random.

The Tie::IxHash module on CPAN can be used to obtain the behaviour
you're after, though.



--
David Precious ("bigpresh") <dav...@preshweb.co.uk>
http://www.preshweb.co.uk/ www.preshweb.co.uk/twitter
www.preshweb.co.uk/linkedin www.preshweb.co.uk/facebook
www.preshweb.co.uk/cpan www.preshweb.co.uk/github


Paul Johnson

unread,
Apr 23, 2013, 7:39:45 AM4/23/13
to kavita kulkarni, begi...@perl.org
On Tue, Apr 23, 2013 at 04:56:55PM +0530, kavita kulkarni wrote:
> Hello All,
>
> 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.

Hashes are unordered, so you cannot directly do what you are asking.

Time effective for whom? If it's for you, take a look at
https://metacpan.org/module/Tie::IxHash

If you're talking about execution speed, go with the simple solution
first and time and profile the solution before looking to optimise it.
You may find that the simple solution is good enough.

--
Paul Johnson - pa...@pjcj.net
http://www.pjcj.net

kavita kulkarni

unread,
Apr 23, 2013, 8:31:56 AM4/23/13
to rc...@pcug.org.au, begi...@perl.org
The values I have stored in the hash should essentially be retrieved in
order, so normal retrieval is not a correct solution for me.

I have tried using IxHash, but it takes much longer execution time than
expected. Any other solution for this?

Regards,
Kavita :-)


On Tue, Apr 23, 2013 at 5:11 PM, Owen <rc...@pcug.org.au> wrote:

>
> > Hello All,
> >
> > 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.
>
>
> Hashes are unordered, have a look at Tie::IxHash in CPAN,
> http://search.cpan.org/~chorny/Tie-IxHash-1.23/lib/Tie/IxHash.pm
>
>
> --
> Owen
>
>

John SJ Anderson

unread,
Apr 23, 2013, 8:36:26 AM4/23/13
to begi...@perl.org
On Tue, Apr 23, 2013 at 5:31 AM, kavita kulkarni
<kavitah...@gmail.com>wrote:

> The values I have stored in the hash should essentially be retrieved in
> order, so normal retrieval is not a correct solution for me.
>
> I have tried using IxHash, but it takes much longer execution time than
> expected. Any other solution for this?
>

Store your data in the hash like normal. Maintain a separate array data
structure with the keys in the order you want. When you need to iterate
over the hash, get the list of keys from the array instead of from calling
keys() on the hash. (This is, I believe, what Tie::IxHash is doing under
the hood; doing it directly without tie() may be faster.)

j.

Octavian Rasnita

unread,
Apr 23, 2013, 8:44:28 AM4/23/13
to Paul Johnson, kavita kulkarni, begi...@perl.org
From: "Paul Johnson" <pa...@pjcj.net>
Or you can also use Tie::Hash::Indexed.

From its POD:

DESCRIPTION
Tie::Hash::Indexed is very similar to Tie::IxHash. However, it is
written completely in XS and usually about twice as fast as Tie::IxHash.

Octavian

kavita kulkarni

unread,
Apr 23, 2013, 9:20:44 AM4/23/13
to Octavian Rasnita, Paul Johnson, begi...@perl.org
Thanks All,

I have prefixed keys with numbers & used hash sorting, that resolved the
issue.

Regards,
Kavita :-)

Andy Bach

unread,
Apr 23, 2013, 12:48:05 PM4/23/13
to kavita kulkarni, Octavian Rasnita, Paul Johnson, begi...@perl.org
On Tue, Apr 23, 2013 at 8:20 AM, kavita kulkarni
<kavitah...@gmail.com>wrote:

>
> I have prefixed keys with numbers & used hash sorting, that resolved the
> issue.


Hmm, why not an array of <something> (e.g. anon. hashes) then? If you're
creating them in order, just push them on an array - got to be cheaper than
sorting hash keys later on, right?

I guess I thought one of the points of a hash (vs an array) was random
access to a particular key/value pair, but if you want sequential access,
that's what arrays give you. My guess is we're missing some part of the
problem you're solving this way.


--

a

Andy Bach,
afb...@gmail.com
608 658-1890 cell
608 261-5738 wk

John Delacour

unread,
Apr 23, 2013, 6:42:37 PM4/23/13
to begi...@perl.org
On 23/4/13 at 12:26, kavitah...@gmail.com (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.

What about this? :

#!/usr/bin/perl
use strict;
my @array;
for (
{length => 120}, # these are references
{width => 120},
{height => 220},
{weight => 12},
{price => 333},
) {
push @array, $_;
}
for (@array) {
for (keys %$_) {print "$_\n"};
}

# JD

kavita kulkarni

unread,
Apr 24, 2013, 1:07:57 AM4/24/13
to John Delacour, begi...@perl.org
Let me try this way as well. (using array)
Will compare the results with the solution I have implemented and will
continue with the one which is faster. :)
Thanks for all your responses!!

Regards,
Kavita :-)
> --
> To unsubscribe, e-mail: beginners-...@perl.org
> For additional commands, e-mail: beginne...@perl.org
> http://learn.perl.org/
>
>
>

Dr.Ruud

unread,
Apr 24, 2013, 5:44:44 AM4/24/13
to begi...@perl.org
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

0 new messages