in reply to first elements in array, split join and sort problem

Couldn't see anything obviously wrong with your wrong with your code, so it's possible that your data contains blank lines as surmised by others.

However, if your data contains blank lines they will get sorted to the top (Ie. first out) of your array, and by discarding them on output you will end up with less than the 10 lines you want. Better I think to discard them on input, which is what the grep in the code below does.

I also thought that building an Array of Arrays in order to sort them was doing things the hard way, so I came up with the sort below. I don't know if it is a variation on one of the named sorts, or simply something I made up, but it works and will (I think, I haven't benchmarked it) be somewhat more efficient than your original.

Another thing I've done is to NOT chomp the lines on input, as you are only going to have to put the newlines back on output.

#! perl -sw use strict; my ($a1,$b1); #open(DATA, "+< tmp/$tmp.dat") or die "file:$tmp $!"; my @lines = sort { do{ ($a1) = $a =~ m/^([^,]+?),/; $a1; } <=> do{ ($b1) = $b =~ m/^([^,]+?),/; $b1; } } grep{ !m/^\s*$/ } <DATA>; print @lines[0 .. (scalar @lines >= 10) ? 9 : $#lines]; __DATA__ -25,AAR ,1303.000,00,1328.000 0,ABO ,5.575,00,5.575 36,ABS ,1243.000,00,1207.000 0,ABY ,122.500,00,122.500 -0.1,ACBD ,3.050,00,3.150 -1.5,ACG ,70.000,00,71.500 -20,ACI ,2459.000,00,2479.000 0,ACID ,4.250,00,4.250 -8,ACO ,1869.000,00,1877.000

Gives

# Output C:\test>201177 -25,AAR ,1303.000,00,1328.000 -20,ACI ,2459.000,00,2479.000 -8,ACO ,1869.000,00,1877.000 -1.5,ACG ,70.000,00,71.500 -0.1,ACBD ,3.050,00,3.150 0,ABY ,122.500,00,122.500 0,ACID ,4.250,00,4.250 0,ABO ,5.575,00,5.575 36,ABS ,1243.000,00,1207.000 C:\test>

If you knew for sure that each file you are sorting would always have at least the number of lines you want to print, then you could do away with the need for any arrays at all and reduce the code to:

#! perl -sw use strict; #open(DATA, "+< tmp/$tmp.dat") or die "file:$tmp $!"; my ($a1,$b1); print + (sort { do{ ($a1) = $a =~ m/^([^,]+?),/; $a1; } <=> do{ ($b1) = $b =~ m/^([^,]+?),/; $b1; } } grep{ !m/^\s*$/ } <DATA>)[0 .. 8];

But that's beginning to look like a golf solution, so probably not worth the effort. However, it did lead me to noticing that in your top snippet you are printing  @lines[0 .. 9], ie. 10 lines but in your second post, the sample data you provided only had 9 lines which would of course be @lines[0 .. 8].

Could this be why you were getting the original problem?


(Would anyone care to venture an opinion as to whether this is a GRT or ST or someother named sort? If not, I'll take the liberty of calling it BUK's fizz:)

Cor! Like yer ring! ... HALO dammit! ... 'Ave it yer way! Hal-lo, Mister la-de-da. ... Like yer ring!