Accessing Hash keys alphabetically

Accessing Hash keys alphabetically

am 13.09.2007 14:10:45 von 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

Re: Accessing Hash keys alphabetically

am 13.09.2007 14:49:13 von Paul Lalli

On Sep 13, 8:10 am, Bill H wrote:
> 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

Re: Accessing Hash keys alphabetically

am 13.09.2007 14:51:30 von Peter Makholm

Bill H writes:

> 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

Re: Accessing Hash keys alphabetically

am 13.09.2007 15:27:20 von Bill H

On Sep 13, 8:49 am, Paul Lalli wrote:
> On Sep 13, 8:10 am, Bill H wrote:
>
> > 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

Re: Accessing Hash keys alphabetically

am 13.09.2007 15:28:35 von Spiros Denaxas

On Sep 13, 1:49 pm, Paul Lalli wrote:
> On Sep 13, 8:10 am, Bill H wrote:
>
> > 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...

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

Re: Accessing Hash keys alphabetically

am 13.09.2007 16:51:18 von Michele Dondi

On Thu, 13 Sep 2007 05:49:13 -0700, Paul Lalli
wrote:

>%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
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^ ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER 256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,

Re: Accessing Hash keys alphabetically

am 13.09.2007 17:16:14 von Paul Lalli

On Sep 13, 10:51 am, Michele Dondi wrote:
> On Thu, 13 Sep 2007 05:49:13 -0700, Paul Lalli
> wrote:
>
> >%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.)

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

Re: Accessing Hash keys alphabetically

am 13.09.2007 23:06:53 von Michele Dondi

On Thu, 13 Sep 2007 08:16:14 -0700, Paul Lalli
wrote:

>> 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

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
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^ ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER 256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,

Re: Accessing Hash keys alphabetically

am 14.09.2007 03:21:55 von Ted Zlatanov

On Thu, 13 Sep 2007 23:06:53 +0200 Michele Dondi wrote:

MD> On Thu, 13 Sep 2007 08:16:14 -0700, Paul Lalli
MD> wrote:

>>> 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

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 :)

Re: Accessing Hash keys alphabetically

am 14.09.2007 07:21:25 von Jim Cochrane

On 2007-09-13, Michele Dondi wrote:
> On Thu, 13 Sep 2007 08:16:14 -0700, Paul Lalli
> wrote:
>
>>> 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
>
> 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!

--

Re: Accessing Hash keys alphabetically

am 14.09.2007 09:01:10 von Dummy

Ted Zlatanov wrote:
> On Thu, 13 Sep 2007 23:06:53 +0200 Michele Dondi wrote:
>
> MD> On Thu, 13 Sep 2007 08:16:14 -0700, Paul Lalli
> MD> wrote:
>
>>>> 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
>
> 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
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall