Retreving keys, values in the order the hash was created?

H

Hemant Shah

Folks,

I have a function that builds a hash in a particular order (not sorted by
keys or values), and returns the hash.

In the calling program how do I traverse the hash in the order it was
created?


%MyHash = CreateHash(arg1, arg2);

I want to retrieve the key, value pair in the same order as the hash
was created.

Thanks.


--
Hemant Shah /"\ ASCII ribbon campaign
E-mail: (e-mail address removed) \ / ---------------------
X against HTML mail
TO REPLY, REMOVE NoJunkMail / \ and postings
FROM MY E-MAIL ADDRESS.
-----------------[DO NOT SEND UNSOLICITED BULK E-MAIL]------------------
I haven't lost my mind, Above opinions are mine only.
it's backed up on tape somewhere. Others can have their own.
 
J

J. Gleixner

Hemant said:
Folks,

I have a function that builds a hash in a particular order (not sorted by
keys or values), and returns the hash.

In the calling program how do I traverse the hash in the order it was
created?


%MyHash = CreateHash(arg1, arg2);

I want to retrieve the key, value pair in the same order as the hash
was created.


perldoc -q "How can I make my hash remember the order I put elements
into it"


Use the Tie::IxHash from CPAN.

use Tie::IxHash;
tie my %myhash, 'Tie::IxHash';
for (my $i=0; $i<20; $i++) {
$myhash{$i} = 2*$i;
}
my @keys = keys %myhash;
# @keys = (0,1,2,3,...)
 
T

Tad McClellan

Hemant Shah said:
$ perldoc -q "How can I make my hash remember the order I put elements into it"
No documentation found for "perlfaq1".
No documentation found for "perlfaq2".
No documentation found for "perlfaq3".
No documentation found for "perlfaq4".
No documentation found for "perlfaq5".
No documentation found for "perlfaq6".
No documentation found for "perlfaq7".
No documentation found for "perlfaq8".
No documentation found for "perlfaq9".


You have a broken installation of perl.

Fix it.
 
M

Malcolm Dew-Jones

Hemant Shah ([email protected]) wrote:

: Folks,

: I have a function that builds a hash in a particular order (not sorted by
: keys or values), and returns the hash.

: In the calling program how do I traverse the hash in the order it was
: created?


: %MyHash = CreateHash(arg1, arg2);

: I want to retrieve the key, value pair in the same order as the hash
: was created.

Hashes don't do that, though there are modules that add this feature to
hashes.

The non-module technique is to maintain both an array and a hash.

my %items;
my @items;

$items{$new_one} = $its_value;
push @items , $new_one;

foreach my $item (@items)
{
print "$item $items{$item} \n";
}

This is not as elegant as a tie'd module, but on the other hand you have
additional control over the ordering and do not need anything other than
vanilla perl.

As for perlfaq entries, your perl setup should have various files with
names like perlfaq1.pod, perlfunc.pod, (i.e. look for perl*.pod). If you
can't find these files anywhere then your perl setup is missing things
(perhaps you should re-install it). If you can find these files then
something like a PATH is wrong cause perl ought to be able to find its own
files, so either figure our what's miss configured in your perl setup, or
re-install perl.

(On this system, perl is installed in /usr/lib/perl5 and many of the
document files are in /usr/lib/perl5/5.6.0/pod. On windows the files are
often in C:\perl\lib\pod . )
 
S

Sherm Pendley

In the calling program how do I traverse the hash in the order it was
created?

You can't. Hashes are, by definition, unordered. If you need to access
elements in a particular order, use an array.

sherm--
 
H

Hemant Shah

While said:
Hemant Shah ([email protected]) wrote:

: Folks,

: I have a function that builds a hash in a particular order (not sorted by
: keys or values), and returns the hash.

: In the calling program how do I traverse the hash in the order it was
: created?


: %MyHash = CreateHash(arg1, arg2);

: I want to retrieve the key, value pair in the same order as the hash
: was created.

Hashes don't do that, though there are modules that add this feature to
hashes.


Thanks. That's whay I did, instead of using Tie module.

The non-module technique is to maintain both an array and a hash.

my %items;
my @items;

$items{$new_one} = $its_value;
push @items , $new_one;

foreach my $item (@items)
{
print "$item $items{$item} \n";
}

This is not as elegant as a tie'd module, but on the other hand you have
additional control over the ordering and do not need anything other than
vanilla perl.

As for perlfaq entries, your perl setup should have various files with
names like perlfaq1.pod, perlfunc.pod, (i.e. look for perl*.pod). If you
can't find these files anywhere then your perl setup is missing things
(perhaps you should re-install it). If you can find these files then
something like a PATH is wrong cause perl ought to be able to find its own
files, so either figure our what's miss configured in your perl setup, or
re-install perl.

(On this system, perl is installed in /usr/lib/perl5 and many of the
document files are in /usr/lib/perl5/5.6.0/pod. On windows the files are
often in C:\perl\lib\pod . )

--
Hemant Shah /"\ ASCII ribbon campaign
E-mail: (e-mail address removed) \ / ---------------------
X against HTML mail
TO REPLY, REMOVE NoJunkMail / \ and postings
FROM MY E-MAIL ADDRESS.
-----------------[DO NOT SEND UNSOLICITED BULK E-MAIL]------------------
I haven't lost my mind, Above opinions are mine only.
it's backed up on tape somewhere. Others can have their own.
 
T

Tassilo v. Parseval

Also sprach Hemant Shah:
Thanks. That's whay I did, instead of using Tie module.

Interesting. How did you do that without using a tie module? Did you
rewrite hv.c and recompile perl?

Tassilo
 
H

Hemant Shah

While said:
Also sprach Hemant Shah:


Interesting. How did you do that without using a tie module? Did you
rewrite hv.c and recompile perl?

I changed my subroutine to build an array of keys in the order I want, and
then return a references to an array and the hash. The calling program then
loops through the array of keys to retrive the values from the hash.


Here is a psuedo code:

# Inside Perl module MyPerlModule.pm
sub MySub
{
my($InHash) = shift;
my($Language) = shift;

my(%SubHash) = ();
my(@SubKeys) = ();

# Code to get information from database.

while (($Col1,$Col2) = $SqlStmtHdl->fetchrow())
{
push(@SubKeys, $Col1);
$SubHash{$Col1} = $Col2;
}

return(\%SubHash, \@SubKeys);
}


# Inside Main Program

($HashRef, $ArrayRef) = MyPerlModule::MySub(\%Parms, 'EN1');

%MyHash = %{$HashRef};
@KeyArray = @{$ArrayRef};

foreach $Key (@KeyArray)
{
print "$Key\t[$MyHash{$Key}]\n";
}


--
Hemant Shah /"\ ASCII ribbon campaign
E-mail: (e-mail address removed) \ / ---------------------
X against HTML mail
TO REPLY, REMOVE NoJunkMail / \ and postings
FROM MY E-MAIL ADDRESS.
-----------------[DO NOT SEND UNSOLICITED BULK E-MAIL]------------------
I haven't lost my mind, Above opinions are mine only.
it's backed up on tape somewhere. Others can have their own.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top