perlquestion
biohisham
Well, I guess I got away with this in some magical way I can't fathom in a Perl 5.12.4, I confess I was being abusive when tried to numerically compare some group of character strings which contained a number with the purpose of sorting them, the guilty feeling of seeing the warnings is irksome. I have a list of files that I want to sort orderly based on the number in their names and I thought I will achieve that through a Schwartzian transform. My files have the format of '<c>sequence<n>.gb.txt'</c> where <c><n></c> is any number.
<p>
What my code does is that it goes around the directory picking these file names and feed that into an array, even though the files are arranged in the directory they are not in that array, so doing
<c>@sorted = map{$_->[0]} sort{$a->[2]<=>$b->[2]} map{[$_,split/sequence/]} @unsorted</c> was my option, trying various combinations to <c>split</c> finally landed me in the direction (I tried splittig around /./ or /\d+/..etc). It is clear that sort() is so generous, I tried <c>cmp</c> (just to test what the output looks like). The code sorts @unsorted and yet complains of '<c>arguments being not numeric in numeric comparison (<=>)' blah blah</c>
</p>
<p>So Perl's sort() gracefully understood what I mean yet I got forgiving-ly pinched,I wonder as to how I can best evade introducing such warnings (going "<c>no warnings</c>" of course is not an option for me;)), any ideas?</p><p></p>
<code>
use strict;
use warnings;
my @unsorted;
my @sorted;
while(my $file = <DATA>){
chomp $file;
push @unsorted, $file;
}
@sorted = map{$_->[0]} sort{$a->[2] <=> $b->[2]} map{[$_, split/sequence/]} @unsorted;
print join("\n",@sorted);
__DATA__
sequence3.gb.txt
sequence1.gb.txt
sequence7.gb.txt
sequence5.gb.txt
sequence2.gb.txt
sequence4.gb.txt
sequence10.gb.txt
sequence9.gb.txt
sequence8.gb.txt
</code>
<c>
##OUTPUT##
Argument "1.gb.txt" isn't numeric in numeric comparison (<=>) at SortQuestion.pl line 11, <DATA> line 9.
...
...
sequence1.gb.txt
sequence2.gb.txt
sequence3.gb.txt
sequence4.gb.txt
sequence5.gb.txt
sequence7.gb.txt
sequence8.gb.txt
sequence9.gb.txt
</c>
<b>UPDATE:</b>Apparently the powers of a Schwartzian ensemble are so crazy <p></p><p></p>
<div class="pmsig"><div class="pmsig-769448">
<hr>
David R. Gergen said "We know that second terms have historically been marred by hubris and by scandal." and I am a two y.o. monk today :D, June,12th, 2011...
</div></div>