Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

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

by choroba (Cardinal)
on Aug 06, 2015 at 09:09 UTC ( [id://1137630]=note: print w/replies, xml ) Need Help??


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

Turn warnings on:
"my $a" used in sort comparison at ./1.pl 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.

لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Replies are listed 'Best First'.
Re^2: question about: my $a AND sort {$a <=> $b} keys %hash ($::a & $::b)
by BrowserUk (Patriarch) on Aug 06, 2015 at 21:04 UTC
    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.
      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re^2: question about: my $a AND sort {$a <=> $b} keys %hash
by rsFalse (Chaplain) on Aug 06, 2015 at 09:19 UTC
    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.

      CountZero

      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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2024-04-19 19:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found