Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gmmastros on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting is not working... someone please help

Status
Not open for further replies.

superyo

Programmer
Apr 26, 2001
1
0
0
US
I have an array whose strings are flatfile db lines.
The first part of each $ looks like this: 12,456|L|etc.
It is a number followed by pipe(|)delimiters and other junk.

I am trying to sort this array by the first one or two #'s, so here is what I have:

@stupidperl = sort {($a =~ /^\d\d?/) <=> ($b =~ /^\d\d?/)} @stupidperl;

this should pull the first one or two digits of the string and sort by them numerically, but it fails to do so. I don't get it. PLEASE HELP ME. I AM SAD.
 
a regular expression in scalar context only returns true or false, depending on whether it matches or not. what you need to do for you sort block is this:[tt]
{(($a =~ /^(\d\d?)/) && $1) <=> (($b =~ /^(\d\d?)/) && $1)}
[/tt]
the '$1' whatever is contained in the first set of parentheses of the last matching regular expression.

you know, there's probly a better way to do this, involving just selecting the first two bytes of the string... &quot;If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito.&quot;
 
ooo, try this:[tt]
{pack(&quot;CC&quot;, unpack(&quot;CC&quot;, $a)) <=> pack(&quot;CC&quot;, unpack(&quot;CC&quot;, $b))}
[/tt]

still, there should be a quicker way to do this... &quot;If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito.&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top