Jump to content

Odd thing in Perl

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
16 replies to this topic

#1
Jojan

Jojan

    Newbie

  • Members
  • Pip
  • 4 posts
Hi. Decided to learn some Perl today and bumped in to something odd.

This code

my %lot = ("a" => 1, "b" => 2, "c" => 3, "d" => 4);


for my $i (%lot) {

    print $i . "\n";

}
results in

c

3

a

1

b

2

d

4

on my home machine (Ubuntu 10.04) with Perl version 5.10. But on my schools Solaris machine with Perl version 5.004 I get more of the result I expect, which is

a

1

b

2

c

3

d

4


So is there a good explanation for this, or is it just some strange feature?

I moved c => 3 to other places in lot (for example my %lot = ("a" => 1, "b" => 2, "d" => 4, "c" => 3); ) but get the same result.

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
In PERL a hash defines no ordering properties. The order in which things come out will be unpredictable.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
Jojan

Jojan

    Newbie

  • Members
  • Pip
  • 4 posts
Thank you for the reply!

Is there a "deeper thought" about this feature? It does not seem to be random, so how have this been implemented? It differs, as I wrote, between versions.

#4
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
I did some digging, perldoc -f keys returns:

Quote

The keys of a hash are returned in an apparently random order. The actual random order is subject to change in future versions of Perl, but it is guaranteed to be the same order as either the values or each function produces (given that the hash has not been modified). Since Perl 5.8.1 the ordering is different even between different runs of Perl for security reasons (see Algorithmic Complexity Attacks in perlsec).

It appears since your 5.004 the internal storing retrieval method has changed a little, but remember to never rely on it (as I hinted before), use sort on the hash:
for my $i (sort keys %hash) {
    print "$i -> $hash{$i}\n";
}

Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#5
Jojan

Jojan

    Newbie

  • Members
  • Pip
  • 4 posts
Interesting.

Since I am new to Perl I just wanted to see what happened.

#6
bythos

bythos

    Newbie

  • Members
  • PipPip
  • 14 posts
I've always through that the hash behavior was a pain if you wanted to iterate through a hash. I wonder how perl6 will handle it?

#7
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts

bythos said:

I've always through that the hash behavior was a pain if you wanted to iterate through a hash. I wonder how perl6 will handle it?

Welcome to the forums!

There isn't going to be much change in that aspect, although I found a nice compiled list of differences in PERL 6 that may be of interest:
Perl6 :: Perl5 :: Differences
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#8
bythos

bythos

    Newbie

  • Members
  • PipPip
  • 14 posts
cheers thanks for the welcome. But i do have to ask the question why is the perl forum under the web development? i don't think perl is used much for web development anymore. more for system administration these days.

#9
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
Hmmm you know what I wonder the same thing too, although now I look at it there aren't any sections it'd "obviously" fit in, I'd vote for the Linux section but that's sort of tight already. Webdev more up top so it gets enough traffic, can't complain there.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#10
Jojan

Jojan

    Newbie

  • Members
  • Pip
  • 4 posts
I assume most people use Ctrl + f (or equivalent) to search for the language they want, and don't really care where it is placed.

#11
lor

lor

    Programming Goddess

  • Members
  • PipPipPipPipPipPipPip
  • 884 posts
Um... Pretty sure any hash set has unreliable ordering, not just in Perl.


#12
bythos

bythos

    Newbie

  • Members
  • PipPip
  • 14 posts
Yeah its depends on the how a hash is implemented in the languages library in most cases.