Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
more useful options
 
PerlMonks  

Re: Re: substr sort ??

by dbwiz (Curate)
on Aug 22, 2003 at 10:32 UTC ( [id://285790]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to Re: substr sort ??
in thread substr sort ??

You could simply use @values = sort @values, since this checks the first two digits

They are not exactly the same thing.

Your method will only work if every element had the same number of digits. If you were to compare "1abc" and "10abc", it would give you the wrong order.

perl -e '@array=(qw(1abc 10abc));@sorted=sort @array; print "@sorted\n +";' 10abc 1abc perl -e '@array=(qw(1abc 10abc)); @sorted = sort {$a<=>$b} @array; print "@sorted\n";' 1abc 10abc

Replies are listed 'Best First'.
Re: Re: Re: substr sort ??
by CombatSquirrel (Hermit) on Aug 22, 2003 at 11:42 UTC
    I know, but the example had zero-padded fixed-width integer identifiers, which suggested that the numbers were always fixed width. I thought about a Schwartzian transform at first (like flounder99 did in Re: substr sort ??), but decided it was overkill and due to the multiple mappings slower than the direct comparison approach. If the identifiers were integers with different numbers of digits, though, my code wouldn't work, as you pointed out.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://285790]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.