Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Using $a and $b for sort

by kirillm (Friar)
on Jan 03, 2008 at 11:48 UTC ( [id://660172]=note: print w/replies, xml ) Need Help??


in reply to Using $a and $b for sort

Sorting a hash table? Do you mean sorting keys in a hash? Or?

If you have lexicalized $a and $b (with my), you won't be able to use them in sort in the same or in the child block:

km@krot:/home/km> perl -le 'my $a = "q"; my $b = "x"; print join "-", +sort { $a cmp $b } qw(foo bar baz); print $a; print $b;' Can't use "my $a" in sort comparison at -e line 1.

If you have lexicalized $a and $b in a separate block, you can use $a and $b in sort (note that both $a and $b are empty in last prints):

km@krot:/home/km> perl -le '{my $a = "q"; my $b = "x";} print join "-" +, sort { $a cmp $b } qw(foo bar baz); print $a; print $b;' bar-baz-foo

Please read perldoc -f sort and perlvar. I'd avoid use of $a and $b altogether.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-04-19 13:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found