Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

perl sort issue while going through article at perl.com

by convenientstore (Pilgrim)
on Oct 21, 2007 at 17:55 UTC ( [id://646282]=perlquestion: print w/replies, xml ) Need Help??

convenientstore has asked for the wisdom of the Perl Monks concerning the following question:

what is wrong with below? I was going through tutorial in perl.com and first I think it has typo where it compares b to b (where it should be a to b and b to a) but also when I run the code itself, it does NOT sort it correctly..
Can you please tell me why this code does not sort it correctly?
http://www.perl.com/pub/a/2006/11/02/all-about-hashes.html #!/usr/bin/perl -w use strict; my @list = qw(oranges oranges apple tomato tomato tomato tomato waterm +elon watermelon watermelon); my %histogram; $histogram{$_}++ for @list; my @unique = keys %histogram; foreach (keys %histogram) { print "$_ , $histogram{$_}\n"; } my @popular = (sort { $histogram{$b} <=> $histogram{$a} } @unique)[0. +.2]; print "@popular\n"; __END__ foreach (keys %histogram) { print "$_ , $histogram{$_}\n"; } [root@myserver chaos]# ./!$ ./perl.hashcrashcourse oranges , 2 watermelon , 3 tomato , 4 apple , 1 tomato watermelon oranges #!/usr/bin/perl -w use strict; my @list = qw(oranges oranges apple tomato tomato tomato tomato waterm +elon watermelon watermelon); my %histogram; $histogram{$_}++ for @list; my @unique = keys %histogram; foreach (keys %histogram) { print "$_ , $histogram{$_}\n"; } my @popular = (sort { $histogram{$a} <=> $histogram{$b} } @unique)[0. +.2]; print "@popular\n"; __END__ foreach (keys %histogram) { print "$_ , $histogram{$_}\n"; } [root@myserver chaos]# ./!$ ././perl.hashcrashcourse oranges , 2 watermelon , 3 tomato , 4 apple , 1 apple oranges watermelon

Replies are listed 'Best First'.
Re: perl sort issue while going through article at perl.com
by shmem (Chancellor) on Oct 21, 2007 at 18:22 UTC
    my @popular = (sort { $histogram{$b} <=> $histogram{$a} } @unique)[0. +.2]; print "@popular\n"; __END__ tomato watermelon oranges

    Correct - hash keys, sorted descending by value, first three ( values are 4,3,2).

    my @popular = (sort { $histogram{$a} <=> $histogram{$b} } @unique)[0. +.2]; print "@popular\n"; __END__ apple oranges watermelon

    Also correct - hash keys, sorted ascending by value, first three (values are 1,2,3).

    If you iterate over the hash keys and print them out with their value, they will come out in any order. A hash isn't sorted.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
      one more question from same tutorial, can you do below in reference hash?
      my %histogram; $histogram{$_}++ for @list;
      I was trying
      foreach (@data1) { $hist{$_}++ for $_; } my @uni = keys %hist; print "@uni\n";
      but above actually prints out the reference address. I tried few different things but does not print out the actual value.. can someone give me a pointer on this(no pun intended).. thanks. for example like below....
      #!/usr/bin/perl -w use strict; use warnings; use Data::Dumper; my $ref = { a => { A => 1, B => 2, C => 3}, b => { D => 1, E => 2, F => 4}, c => { G => 2, H => 1, I => 2}, }; #print Dumper($ref); my %hist; foreach ($ref) { $hist{$_}++ for $_; } my @uni = keys %hist; print "@uni\n";
        foreach ($ref) { $hist{$_}++ for $_; }

        Your $ref is a hash reference - use keys:

        for ( keys %$ref) { ... }

        Since your data structure is a HoH, inside that loop $_ is, again, a hash reference. Again you need keys.

        $hist{$_}++ for keys %$_;

        But such usage of $_ is a bit obfuscated. Better say

        for my $hashref ( keys %$ref) { $hist{$_}++ for keys %$hashref; }

        --shmem

        _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                      /\_¯/(q    /
        ----------------------------  \__(m.====·.(_("always off the crowd"))."·
        ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: perl sort issue while going through article at perl.com
by duff (Parson) on Oct 21, 2007 at 17:59 UTC

    Er, how does it not sort correctly? If I'm missing something obvious, please highlight it for me.

      you are right. I was thinking that it should have been oranges, watermelon, tomato instead apple, oranges, watermelon stupid thinking... my bad.. thanks for pointing this out

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (4)
As of 2024-04-25 06:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found