Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re: Re: Re: Stuck while learning about the hash

by andreychek (Parson)
on Jun 08, 2001 at 08:32 UTC ( #86841=note: print w/replies, xml ) Need Help??

in reply to Re: Re: Stuck while learning about the hash
in thread Stuck while learning about the hash

> Not only is that 6 of each number instead of 3, but the order is all messed up. Any idea what's going on here?

A quick note about order -- arrays will stay in the order you assign them in, but hashes may not. If you assign
$array[0] = "foo"
Then the value foo will always be in the first position in the array, unless you later reassign it in some way.

However, hashes are a bit different. Whenever you have a hash, Perl put's them in whatever order it finds easiest to store them in. So if you assign:

$hash{foo} = "abc"; $hash{bar} = "xyz";
and you later run:
@somearray = keys %hash
You cannot expect the key 'foo' to be returned first and 'bar' to be second. The only way to guarantee your output here is to run the command through something like sort:
@somearray = sort(keys %hash);

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://86841]
[choroba]: https://metacpan. org/author/ SIMBABQUE might know. He's a heavy Ingress player.
[Corion]: choroba: Yeah, I know :) I might ask him at the GPW :))
hippo giggles at the thought of people "playing" Ingres

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2018-03-19 09:14 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (236 votes). Check out past polls.