A different approach is needed in order to sort a tab-delimited flat file. Our reference book reveals a useful function, split, which can change a line of text into an array of strings, dividing the line based on a key character or string. In our case, we want to split the line based on the tab character, identified in Perl by '\t'.
Using split, we can sort our file using a uniquely different sorting approach:
sub specificSort2
{
@first = split( '\t', $a );
@second = split( '\t', $b );
$compare = ( $first[2] cmp $second[2] ); # job number
if ( $compare != 0 ) { return ( $compare ); }
$compare = ( $first[6] cmp $second[6] ); # period
if ( $compare != 0 ) { return ( $compare ); }
$compare = ( $first[3] cmp $second[3] ); # product
if ( $compare != 0 ) { return ( $compare ); }
$compare = ( $first[4] <=> $second[4] ); # system price
if ( $compare != 0 ) { return ( $compare ); }
$compare = ( $first[5] <=> $second[5] ); # net price
return ( $compare );
}
Using split, we can sort our file using a uniquely different sorting approach:
sub specificSort2
{
@first = split( '\t', $a );
@second = split( '\t', $b );
$compare = ( $first[2] cmp $second[2] ); # job number
if ( $compare != 0 ) { return ( $compare ); }
$compare = ( $first[6] cmp $second[6] ); # period
if ( $compare != 0 ) { return ( $compare ); }
$compare = ( $first[3] cmp $second[3] ); # product
if ( $compare != 0 ) { return ( $compare ); }
$compare = ( $first[4] <=> $second[4] ); # system price
if ( $compare != 0 ) { return ( $compare ); }
$compare = ( $first[5] <=> $second[5] ); # net price
return ( $compare );
}
No comments:
Post a Comment