Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Using $a and $b for sort

by kirillm (Friar)
on Jan 03, 2008 at 11:48 UTC ( #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? | Other CB clients
Other Users?
Others studying the Monastery: (5)
As of 2022-08-10 22:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?