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

  2. Bill H

    Paul Lalli Guest

    %ARRAY is a really bad name for a *hash*, IMO...
    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.

    ^^^^ ^^^^^

    You've just made this into a Self-Answering Question. :)
    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. Advertisements

  3. This is the same as

    @dbf = keys %ARRAY;

    (assuming @dbf is empty)
    And no need to store the itermediate data. So just do

    @rbf = sort keys %ARRAY;
    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

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

    Bill H
     
    Bill H, Sep 13, 2007
    #4
  5. 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
     
    Spiros Denaxas, Sep 13, 2007
    #5
  6. 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
     
    Michele Dondi, Sep 13, 2007
    #6
  7. Bill H

    Paul Lalli Guest

    It is. You speak English better than about 75% of the native-speakers
    here. No worries. :p
    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. 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
     
    Michele Dondi, Sep 13, 2007
    #8
  9. Bill H

    Ted Zlatanov Guest

    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

    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. How do you play an avocado? I thought it was an ocarina. (Sorry, I only
    speak Canadian!)


    John
     
    John W. Krahn, Sep 14, 2007
    #11
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.