Accessing Hash keys alphabetically

Discussion in 'Perl Misc' started by Bill H, Sep 13, 2007.

  1. Bill H

    Bill H Guest

    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
     
    Bill H, Sep 13, 2007
    #1
    1. Advertising

  2. Bill H

    Paul Lalli Guest

    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
     
    Paul Lalli, Sep 13, 2007
    #2
    1. Advertising

  3. 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
     
    Peter Makholm, Sep 13, 2007
    #3
  4. Bill H

    Bill H Guest

    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
     
    Bill H, Sep 13, 2007
    #4
  5. 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
     
    Spiros Denaxas, Sep 13, 2007
    #5
  6. 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^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
     
    Michele Dondi, Sep 13, 2007
    #6
  7. Bill H

    Paul Lalli Guest

    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
     
    Paul Lalli, Sep 13, 2007
    #7
  8. 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^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
     
    Michele Dondi, Sep 13, 2007
    #8
  9. Bill H

    Ted Zlatanov Guest

    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 :)
     
    Ted Zlatanov, Sep 14, 2007
    #9
  10. Bill H

    Jim Cochrane Guest

    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!

    --
     
    Jim Cochrane, Sep 14, 2007
    #10
  11. 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
     
    John W. Krahn, Sep 14, 2007
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rp
    Replies:
    1
    Views:
    579
    red floyd
    Nov 10, 2011
  2. Xeno Campanoli
    Replies:
    16
    Views:
    286
    Martin DeMello
    Aug 25, 2005
  3. Alex Fenton

    Hash#values and Hash#keys order

    Alex Fenton, Apr 7, 2006, in forum: Ruby
    Replies:
    1
    Views:
    161
    George Ogata
    Apr 15, 2006
  4. Mage

    hash.keys and hash.values

    Mage, Aug 13, 2006, in forum: Ruby
    Replies:
    14
    Views:
    192
  5. Ronald Fischer

    Hash#keys, Hash#values order question

    Ronald Fischer, Aug 23, 2007, in forum: Ruby
    Replies:
    0
    Views:
    172
    Ronald Fischer
    Aug 23, 2007
Loading...

Share This Page