FAQ
Hi,

I am trying to learn hashes in perl. I created an hash
variable with 4 keys. I tried printing the keys in the hash by:(hash
variable is %machines)

foreach $key (keys %machines) {
print "$key\n";
}

I noticed that the order in which the keys are printed is different
from the order in which I stored them. Is that expected ? If so, what
determines the order.

Another question is that, when I tried something like:

my @my_machines = %machines;
print "@my_machines\n";

I noticed that the output is exactly the reverse of the order in which
I had stored them in the hash. Is this also the expected behaviour ?
If so what is the reason behind that ?

Can someone please help me with these 2 newbie questions ?


PS: I am used to program. So, if I am doing something that is not the
perl way of doing it, please do let me know. Thanks


--
Thank you
Balachandran Sivakumar

Arise Awake and stop not till the goal is reached.

Mail: [email protected]
Blog: http://benignbala.wordpress.com/

Search Discussions

  • Robert Wohlfarth at Apr 6, 2011 at 2:06 pm

    On Wed, Apr 6, 2011 at 8:22 AM, Balachandran Sivakumar wrote:

    I am trying to learn hashes in perl. I created an hash
    variable with 4 keys. I tried printing the keys in the hash by:(hash
    variable is %machines)

    foreach $key (keys %machines) {
    print "$key\n";
    }
    In the perlvar documentation<http://perldoc.perl.org/perldata.html#List-value-constructors>,
    there's a little blurb that says *Note that just because a hash is
    initialized in that order doesn't mean that it comes out in that order. See
    sort <http://perldoc.perl.org/functions/sort.html> for examples of how to
    arrange for an output ordering.* It's easy to miss if you didn't know to
    look for it.

    My limited understanding is that Perl handles the order internally. Code
    should never rely on a specific order.

    --
    Robert Wohlfarth
  • Shawn H Corey at Apr 6, 2011 at 3:31 pm

    On 11-04-06 10:06 AM, Robert Wohlfarth wrote:
    On Wed, Apr 6, 2011 at 8:22 AM, Balachandran Sivakumar<[email protected]
    wrote:
    I am trying to learn hashes in perl. I created an hash
    variable with 4 keys. I tried printing the keys in the hash by:(hash
    variable is %machines)

    foreach $key (keys %machines) {
    print "$key\n";
    }
    In the perlvar documentation<http://perldoc.perl.org/perldata.html#List-value-constructors>,
    there's a little blurb that says *Note that just because a hash is
    initialized in that order doesn't mean that it comes out in that order. See
    sort<http://perldoc.perl.org/functions/sort.html> for examples of how to
    arrange for an output ordering.* It's easy to miss if you didn't know to
    look for it.

    My limited understanding is that Perl handles the order internally. Code
    should never rely on a specific order.
    Hash lookups are faster if the keys do not maintain any order. If you
    add more elements, you will find that the order of the keys will change.
    One thing Perl does guarantee is that the order of keys and values
    will be the same provided you do not add or remove anything from the
    hash. This allows things like this: inserting one hash in another.

    #!/usr/bin/env perl

    use strict;
    use warnings;

    use Data::Dumper;

    # Make Data::Dumper pretty
    $Data::Dumper::Sortkeys = 1;
    $Data::Dumper::Indent = 1;

    # Set maximum depth for Data::Dumper, zero means unlimited
    local $Data::Dumper::Maxdepth = 0;

    # TBD

    my %src_hash = (
    a => 1,
    b => 2,
    c => 3,
    );

    my %dst_hash = (
    x => -1,
    y => -2,
    z => -3,
    );

    print 'BEFORE: ', Dumper \%src_hash, \%dst_hash;

    @dst_hash{keys %src_hash} = values %src_hash;

    print 'AFTER: ', Dumper \%src_hash, \%dst_hash;

    __END__

    --
    Just my 0.00000002 million dollars worth,
    Shawn

    Confusion is the first step of understanding.

    Programming is as much about organization and communication
    as it is about coding.

    The secret to great software: Fail early & often.

    Eliminate software piracy: use only FLOSS.
  • Balachandran Sivakumar at Apr 6, 2011 at 5:51 pm

    On Wed, Apr 6, 2011 at 11:09 PM, Shawn H Corey wrote:


    Thanks a lot for all the replies, links and code snippets. I was
    trying out a few examples from a few websites. And, I was also not
    aware of perldoc until a few hours back. That does seem to be a good
    way to provide help. Just starting out on perl, and so will get back
    with more newbie questions.

    Also, I plan to start off using the Llama book. I would like to know
    if that is a correct approach or is there a better book to start off
    with? Thanks
    You should choose "Reply All" since this message was sent to me only. Now
    you have to re-enter the list address to get this there.  :)
    Oops! The reply mail went only to Shawn. I am sorry about that.
    I assumed that the Reply-To header would be set to the list address :(
    Should have been more careful. I was just asking if starting off with
    the Llama book is a good start or should I be starting off with some
    other book( and of course the Internet). Thanks

    --
    Thank you
    Balachandran Sivakumar

    Arise Awake and stop not till the goal is reached.

    Mail: [email protected]
    Blog: http://benignbala.wordpress.com/
  • Shlomi Fish at Apr 6, 2011 at 3:32 pm
    Hi Balachandran,
    On Wednesday 06 Apr 2011 16:22:44 Balachandran Sivakumar wrote:
    Hi,

    I am trying to learn hashes in perl. I created an hash
    variable with 4 keys. I tried printing the keys in the hash by:(hash
    variable is %machines)

    foreach $key (keys %machines) {
    print "$key\n";
    }

    I noticed that the order in which the keys are printed is different
    from the order in which I stored them. Is that expected ? If so, what
    determines the order.
    The order is determined by perl 5's internal hashing implementation and cannot
    be relied on. If you want a particular order use an array, sort the keys first
    or use a CPAN module. For more information see the resources on:

    http://perl-begin.org/topics/hashes/

    What you can rely on is for the order of the keys in the hash to remain the
    same as long as it is not modified and this is the order in keys(), values()
    and each().
    Another question is that, when I tried something like:

    my @my_machines = %machines;
    print "@my_machines\n";

    I noticed that the output is exactly the reverse of the order in which
    I had stored them in the hash. Is this also the expected behaviour ?
    If so what is the reason behind that ?

    Can someone please help me with these 2 newbie questions ?


    PS: I am used to program. So, if I am doing something that is not the
    perl way of doing it, please do let me know. Thanks
    Also see:

    http://www.nntp.perl.org/group/perl.beginners/2011/03/msg116305.html

    Regards,

    Shlomi Fish

    --
    -----------------------------------------------------------------
    Shlomi Fish http://www.shlomifish.org/
    Apple Inc. is Evil - http://www.shlomifish.org/open-source/anti/apple/

    I invented the term Object-Oriented, and I can tell you I did not have C++ in
    mind. -- Alan Kay (Attributed)

    Please reply to list if it's a mailing list post - http://shlom.in/reply .

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedApr 6, '11 at 1:23p
activeApr 6, '11 at 5:51p
posts5
users4
websiteperl.org

People

Translate

site design / logo © 2023 Grokbase