Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

question about: my $a AND sort {$a <=> $b} keys %hash

by rsFalse (Hermit)
on Aug 06, 2015 at 08:58 UTC ( #1137627=perlquestion: print w/replies, xml ) Need Help??

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

Why doesn't sort? :/
#!usr/bin/perl my $a = 1; %hash = map { $_ , undef } 0 .. 3; @h = sort {$a <=> $b} keys %hash; print "@h\n"; __END__ 0 3 2 1
@h = sort {$a <=> $b} @arr = keys %hash; # doesn't sort @h = sort {$a <=> $b} 0 .. 3; # do sort @h = sort {$a <=> $b} @arr = 0 .. 3; # do sort

Replies are listed 'Best First'.
Re: question about: my $a AND sort {$a <=> $b} keys %hash
by choroba (Archbishop) on Aug 06, 2015 at 09:09 UTC
    Turn warnings on:
    "my $a" used in sort comparison at ./ line 7.

    That's why you should never use $a and $b as variable names. sort needs access to the package variable $a (documented at $a).

    Update: link to perlvar added.

    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      That's why you should never use $a and $b as variable names.

      Personally, I think instead of promoting a blanket ban on using lexical $a & $b -- which 9x% of code can use safely and usefully -- it would be better to change the documentation to show the use of:

      my @sorted = sort{ $::a <=> $::b } @array;

      which avoids the problem and clearly marks out the special usage of these variables for sort.

      A simple change that would easily fix the problem.

      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
      In the absence of evidence, opinion is indistinguishable from prejudice.
      I'm with torvalds on this Agile (and TDD) debunked I told'em LLVM was the way to go. But did they listen!
        It doesn't work in a package, though.
        لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      If write physical formula with acceleration or length of the edge of square, human often use $a as variable. Sad about reservation of $a. I have a bad habit of using "a" from other languages.
        There is no better time than "now" to unlearn bad habits.


        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

        My blog: Imperial Deltronics
Re: question about: my $a AND sort {$a <=> $b} keys %hash
by Discipulus (Abbot) on Aug 06, 2015 at 09:13 UTC
    you must receive an error even without warnings enabled:
    Can't use "my $a" in sort comparison at line 4.
    In short, never use $a and $b in your programs. It is explicitly mentioned in perlvar
    $a $b Special package variables when using sort(), see sort. Because of this + specialness $a and $b don't need to be declared (using use vars , or + our()) even when using the strict 'vars' pragma. Don't lexicalize th +em with my $a or my $b if you want to be able to use them in the sort +() comparison block or function.

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
      Don't lexicalize them with my $a or my $b if you want to be able to us +e them in the sort() comparison block or function.
      But, on the other hand you can localize them when needed:
      $ perl -we ' local $a = 1; @h = sort {$a <=> $b} keys %hash; print "@h\n";> %hash = map { $_ , undef } 0 .. 3; @h = sort {$a <=> $b} keys %hash; print "@h\n"; ' 0 1 2 3
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2020-08-12 03:41 GMT
Find Nodes?
    Voting Booth?
    Which rocket would you take to Mars?

    Results (64 votes). Check out past polls.