Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Using $a and $b for sort

by Antony Lewis (Initiate)
on Jan 03, 2008 at 11:18 UTC ( [id://660159]=perlquestion: print w/replies, xml ) Need Help??

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

Hey all,,, I have got some confusion with the usage of $a and $b for sorting a hash table, as i am using the same variables for some other calculations in the script. Appreciate your response...

Replies are listed 'Best First'.
Re: Using $a and $b for sort
by moritz (Cardinal) on Jan 03, 2008 at 11:34 UTC
    The short answer: don't use $a and $b for anything except sorting and the like.

    They are an exception in that they are not subject to use strict;, so it's best to avoid them.

      Thanks for the response, That makes sense... I did that, but what in case if you want to change these default variables??? Any clue???
        if there is some strange reason you cannot lexify your other usages of $a and $b, then you can consider using a named ($$)-prototyped sort subroutine, and perl will pass the sort variables in @_ instead of package variables.
        sub mysort ($$) { $_[0]->{key} <=> $_[1]->{key} } my @sorted = sort mysort @unsorted;
Re: Using $a and $b for sort
by jbert (Priest) on Jan 03, 2008 at 12:01 UTC
    Avoiding $a and $b in perl is particularly good advice (due to the overlap with the sort magic variables).

    But it's also a good idea to avoid them since they aren't meaningful names. I think one of the most important parts of writing code is choosing good names for variables (and functions, and all name-able things). I don't mean needlessly long names, but appropriate names (name length should in general be loosely proportional to variable scope - if that gives you long names you want smaller scope for your variables).

    Yes, you have to stop and think about a good name, but it makes the code much more readable. Compare: $page->render(); with $tmp->do_it().

    It's also important to change names if you change the code, so they stay accurate. (e.g. change a variable from $employee to $person if you make a routine more general).

Re: Using $a and $b for sort
by kirillm (Friar) on Jan 03, 2008 at 11:48 UTC

    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: perlquestion [id://660159]
Approved by marto
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: (3)
As of 2024-04-19 04:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found