Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Odd thing in Perl


  • Please log in to reply
17 replies to this topic

#1 Jojan

Jojan

    CC Lurker

  • Just Joined
  • Pip
  • 4 posts

Posted 17 August 2010 - 08:13 AM

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.
  • 0

#2 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 18 August 2010 - 05:39 PM

In PERL a hash defines no ordering properties. The order in which things come out will be unpredictable.
  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#3 Jojan

Jojan

    CC Lurker

  • Just Joined
  • Pip
  • 4 posts

Posted 19 August 2010 - 05:56 AM

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.
  • 0

#4 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 20 August 2010 - 05:02 AM

I did some digging, perldoc -f keys returns:

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";
}

  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#5 Jojan

Jojan

    CC Lurker

  • Just Joined
  • Pip
  • 4 posts

Posted 20 August 2010 - 05:29 AM

Interesting.

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

#6 bythos

bythos

    CC Newcomer

  • Just Joined
  • PipPip
  • 14 posts

Posted 21 August 2010 - 02:15 PM

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?
  • 0

#7 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 21 August 2010 - 02:24 PM

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
  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#8 bythos

bythos

    CC Newcomer

  • Just Joined
  • PipPip
  • 14 posts

Posted 21 August 2010 - 08:29 PM

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.
  • 0

#9 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 21 August 2010 - 11:33 PM

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.
  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#10 Jojan

Jojan

    CC Lurker

  • Just Joined
  • Pip
  • 4 posts

Posted 22 August 2010 - 02:59 AM

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.
  • 0

#11 lor

lor

    Programming Goddess

  • Senior Member
  • PipPipPipPipPipPip
  • 497 posts

Posted 05 September 2010 - 02:03 AM

Um... Pretty sure any hash set has unreliable ordering, not just in Perl.
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#12 bythos

bythos

    CC Newcomer

  • Just Joined
  • PipPip
  • 14 posts

Posted 08 September 2010 - 01:04 PM

Yeah its depends on the how a hash is implemented in the languages library in most cases.
  • 0