Accessing Hash keys alphabetically

B

Bill H

Is there any way of accessing a Hash's keys alphabetically? For
example, if I have the following:

$ARRAY{'ABC'} = "ABC";
$ARRAY{'CDE'} = "CDE";
$ARRAY{'BCD'} = "BCD";

and try to access them with a construct like this:

foreach $temp (keys(%ARRAY))
{
print "$ARRAY{$temp}\n";
}

The order I get them when I print seems to be random, or maybe FIFO,
but the way I would want to get them back would be in order based on a
sort of the keys. The way I have gotten around this is to use the
following, which could be improved with a hammer I am sure:

foreach $temp (keys(%ARRAY))
{
$dbf[@dbf] = $temp;
}

@rbf = sort @dbf;

foreach $temp (@rbf)
{
print "$ARRAY{$rbf[0]}\n";
}

This is just sample code but illustrates what I am trying to
accomplish. I am sure there is some perlish way of doing this with a
few curly braces and slashes, but can't seem to figure it out.

Bill H
 
P

Paul Lalli

Is there any way of accessing a Hash's keys alphabetically? For
example, if I have the following:

$ARRAY{'ABC'} = "ABC";
$ARRAY{'CDE'} = "CDE";
$ARRAY{'BCD'} = "BCD";

%ARRAY is a really bad name for a *hash*, IMO...
and try to access them with a construct like this:

foreach $temp (keys(%ARRAY))
{
print "$ARRAY{$temp}\n";

}

The order I get them when I print seems to be random

Random to you and I. Not to Perl. keys(), values(), each(), and hash-
flattening return the hash elements in the internal order used to
store the hash. Which has nothing to do with the order in which you
built the hash.

but the way I would want to get them back would be in order based
on a sort of the keys.

^^^^ ^^^^^

You've just made this into a Self-Answering Question. :)
foreach $temp (keys(%ARRAY))
{
$dbf[@dbf] = $temp;
}

@rbf = sort @dbf;

foreach $temp (@rbf)
{
print "$ARRAY{$rbf[0]}\n";
}

You're doing unnecessary steps. Why? Just sort the keys as you're
iterating through them.

foreach my $key (sort keys %ARRAY) {
print "$ARRAY{$key}\n";
}

Paul Lalli
 
P

Peter Makholm

Bill H said:
foreach $temp (keys(%ARRAY))
{
$dbf[@dbf] = $temp;
}

This is the same as

@dbf = keys %ARRAY;

(assuming @dbf is empty)
@rbf = sort @dbf;

And no need to store the itermediate data. So just do

@rbf = sort keys %ARRAY;
foreach $temp (@rbf)
{
print "$ARRAY{$rbf[0]}\n";
}

That doesn't work - you keep printing the same vaule. But inserting
the above and doing the right thing

for my $temp (sort keys %ARRAY) {
print "$ARRAY{$temp}\n";
}

So, these three lines should do the job.

//Makholm
 
B

Bill H

Is there any way of accessing a Hash's keys alphabetically? For
example, if I have the following:
$ARRAY{'ABC'} = "ABC";
$ARRAY{'CDE'} = "CDE";
$ARRAY{'BCD'} = "BCD";

%ARRAY is a really bad name for a *hash*, IMO...
and try to access them with a construct like this:
foreach $temp (keys(%ARRAY))
{
print "$ARRAY{$temp}\n";

The order I get them when I print seems to be random

Random to you and I. Not to Perl. keys(), values(), each(), and hash-
flattening return the hash elements in the internal order used to
store the hash. Which has nothing to do with the order in which you
built the hash.
but the way I would want to get them back would be in order based
on a sort of the keys.

^^^^ ^^^^^

You've just made this into a Self-Answering Question. :)
foreach $temp (keys(%ARRAY))
{
$dbf[@dbf] = $temp;
}
@rbf = sort @dbf;
foreach $temp (@rbf)
{
print "$ARRAY{$rbf[0]}\n";
}

You're doing unnecessary steps. Why? Just sort the keys as you're
iterating through them.

foreach my $key (sort keys %ARRAY) {
print "$ARRAY{$key}\n";

}

Paul Lalli

As Homer Simpson says "D'0h!" Thanks Paul!

Bill H
 
S

Spiros Denaxas

%ARRAY is a really bad name for a *hash*, IMO...

I totally agree. I was reall disappointed when I came across a perl
tutorial page part of a university module's page and he also used
%ARRAY.

Spiros
and try to access them with a construct like this:
foreach $temp (keys(%ARRAY))
{
print "$ARRAY{$temp}\n";

The order I get them when I print seems to be random

Random to you and I. Not to Perl. keys(), values(), each(), and hash-
flattening return the hash elements in the internal order used to
store the hash. Which has nothing to do with the order in which you
built the hash.
but the way I would want to get them back would be in order based
on a sort of the keys.

^^^^ ^^^^^

You've just made this into a Self-Answering Question. :)
foreach $temp (keys(%ARRAY))
{
$dbf[@dbf] = $temp;
}
@rbf = sort @dbf;
foreach $temp (@rbf)
{
print "$ARRAY{$rbf[0]}\n";
}

You're doing unnecessary steps. Why? Just sort the keys as you're
iterating through them.

foreach my $key (sort keys %ARRAY) {
print "$ARRAY{$key}\n";

}

Paul Lalli
 
M

Michele Dondi

%ARRAY is a really bad name for a *hash*, IMO...

Well, it is and maybe it isn't, but just to play the devil's advocate.
(Don't know if the English idiom is correct.) In fact we call them
*hashes*, but that's just because we refer to their implementation:
more correctly they are "associative arrays". Indeed "hash" is more
appealing.


Michele
 
P

Paul Lalli

Well, it is and maybe it isn't, but just to play the devil's
advocate. (Don't know if the English idiom is correct.)

It is. You speak English better than about 75% of the native-speakers
here. No worries. :p
In fact we call them
*hashes*, but that's just because we refer to their implementation:
more correctly they are "associative arrays". Indeed "hash" is more
appealing.

Yes, but we only call regular arrays "arrays". They have no other
name. So if someone simply says to you "array", you're 99% likely to
think "array", not "associative array" or "hash".

Paul Lalli
 
M

Michele Dondi

It is. You speak English better than about 75% of the native-speakers
here. No worries. :p

Thank you for your kind words. Yet, I occasionally have problems with
idiomatic forms, and in doubt I ask... you know, just to be sure and
*learn*.


Michele
 
T

Ted Zlatanov

MD> Thank you for your kind words. Yet, I occasionally have problems with
MD> idiomatic forms, and in doubt I ask... you know, just to be sure and
MD> *learn*.

Don't listen to Paul, he's having fun at your expense. The proper idiom
is "to play the Devil's avocado." Even native speakers get it wrong
very often!

Ted :)
 
J

Jim Cochrane

Thank you for your kind words. Yet, I occasionally have problems with
idiomatic forms, and in doubt I ask... you know, just to be sure and
*learn*.


Michele

I've been reading your posts for the past several weeks and I didn't
even realize that you were Italian until I saw Paul's response to your
post, then noticed your last name and then your address. You had me
fooled :) - I thought you were a native English speaker!

--
 
J

John W. Krahn

Ted said:
MD> Thank you for your kind words. Yet, I occasionally have problems with
MD> idiomatic forms, and in doubt I ask... you know, just to be sure and
MD> *learn*.

Don't listen to Paul, he's having fun at your expense. The proper idiom
is "to play the Devil's avocado." Even native speakers get it wrong
very often!

How do you play an avocado? I thought it was an ocarina. (Sorry, I only
speak Canadian!)


John
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top