Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Can't figure out the realtive ordering.

by Siddharth (Initiate)
on Sep 26, 2011 at 02:28 UTC ( #927784=perlquestion: print w/ replies, xml ) Need Help??
Siddharth has asked for the wisdom of the Perl Monks concerning the following question:

Hi everyone, I am new to perl and have been going through some programs. The following code dealing with hash and array is has really confused me.

I can't understand what determines the relative ordering of elements.

In the output, why 'joe' comes between 'giggle' and 'dingbat'.

D: giggles 870 joe 2983 dingbat 33 mike -94 fred 44

@alex = ( fred => 44, 'joe' => 2983, mike => -94 ); %heather = ('This', 'will', 'actually', 'work'); %heather = @alex; print "C: [$heather{'fred'}] [$heather{'mike'}] [$heather('joe')] ", "[$heather{'This'}]\n"; $heather{'dingbat'} = 33; $heather{'giggles'} = 870; @alex = %heather; print "D: @alex\n";
C: [44] [-94] [('joe')] [] D: giggles 870 joe 2983 dingbat 33 mike -94 fred 44

Comment on Can't figure out the realtive ordering.
Select or Download Code
Re: Can't figure out the realtive ordering.
by MidLifeXis (Prior) on Sep 26, 2011 at 03:01 UTC

    A hash is a set of unordered key/value pairs. If you do not change the hash, its ordering is stable. See keys and values for why this is important.

    --MidLifeXis

Re: Can't figure out the realtive ordering.
by onelesd (Pilgrim) on Sep 26, 2011 at 04:20 UTC

    You really need to ...

    use strict ; use warnings ;

    ... at the top of your script, and then fix the errors that come up. This will take you on a wonderful journey and go a long way toward solving your problem.

Re: Can't figure out the realtive ordering.
by duyet (Scribe) on Sep 26, 2011 at 06:26 UTC

    The order of an array is fixed, eg. $alex[1] is always 44. The order of an hash is not fixed! See perlintro for more info about hash/array.

    You should use strict and warning in your code. There is an error in the code: $heather('joe') should be $heather{'joe'}, since you defined an hash %heather but there is no array @heather

    Use Dumper to look at the data.

    use strict; use warnings; use Data::Dumper; my @alex = ( fred => 44, 'joe' => 2983, mike => -94 ); print Dumper( \@alex ); my %heather = ('This', 'will', 'actually', 'work'); print Dumper( \%heather ); %heather = @alex; print Dumper( \%heather ); print "C: [$heather{'fred'}] [$heather{'mike'}] [$heather{'joe'}] ", " +[$heather{'This'}]\n"; $heather{'dingbat'} = 33; $heather{'giggles'} = 870; print Dumper( \%heather ); @alex = %heather; print Dumper( \@alex ); print "D: @alex\n";
Re: Can't figure out the realtive ordering.
by sundialsvc4 (Monsignor) on Sep 26, 2011 at 13:01 UTC

    The elements of a hash can be obtained in some-sorted order using the each() function, with certain limitations.   And, you will sometimes see while loops which iterate over sort keys(hashvar).

    The elements of an array do not change position, unless elements are inserted or removed.   The ordering of a hash should be considered by you to be “unpredictable.”   (A hash is designed to champion rapid and consistent access-speed, and retrieval order is the price that you pay.)

Re: Can't figure out the realtive ordering.
by Siddharth (Initiate) on Sep 27, 2011 at 08:47 UTC

    Thanks everyone for helping. I got the point.
    I will surely use strict and warnings from now on.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://927784]
Approved by keszler
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2014-07-30 22:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (241 votes), past polls