Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

( [id://3333] : superdoc . print w/replies, xml ) Need Help??
What version of Perl are you using? This works for me:
#!/usr/bin/perl -w use strict; my $sortname = shift; print join ' ', sort $sortname 1..20; sub num { $a <=> $b } sub alpha { $a cmp $b }
Save this as foo.pl, and...
% foo.pl num 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 % foo.pl alpha 1 10 11 12 13 14 15 16 17 18 19 2 20 3 4 5 6 7 8 9
This is consistent w/ the sort docs:
SUBNAME may be a scalar variable name (unsubscripted), in which case the value provides the name of (or a reference to) the actual subroutine to use. In place of a SUBNAME, you can provide a BLOCK as an anonymous, in-line sort subroutine.
However, I do seem to remember running into your situation in the past. I don't know what version of Perl I was using then; probably 5.004 or some such. I'm now using 5.005_03, which could explain it.

If you are using a real database, though, I would suggest that you try using "order by", as it's likely to be faster than anything you can do yourself, particularly if you have your columns indexed.


In reply to RE: Dynamic sort algorithm selection by btrott
in thread Dynamic sort algorithm selection by Sentinel

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.