sort and keep the latest version

K

Katie

I have this sub, and after the sort, it keeps the last line of simliar
FEATURE lines. However, the first line is the latest version, not the
last line. How can I change to have it keeps the first line instead of
the last line?
------------------------------------------------------------------------------------------------------------------------------
sub lineSort {
($mode, @list) = @_;
local (@sortedList, @cleanList);
@sortedList = sort( @list );

for ( $i = 0; $i < $#sortedList; $i++ ) {
($l1_f1, $l1_f2, $l1_f3, $l1_f4) = split( /[ \t]+/, $sortedList[$i]);
($l2_f1, $l2_f2, $l2_f3, $l2_f4) = split( /[ \t]+/,
$sortedList[$i+1]);
LINESORT: {
if ($mode eq "SERVER" ) { # Compare on 2nd & 3rd fields - name &
hostid
($l1_f2 ne $l2_f2 && $l1_f3 ne $l2_f3) &&
push( @cleanList, $sortedList[$i] );
last LINESORT;
}
if ($mode eq "FEATURE") { # Compare on 2nd field - feature name
( $l1_f2 ne $l2_f2 ) && push( @cleanList, $sortedList[$i] );
last LINESORT;
}
}}
#
# Always save the last line
#
push( @cleanList, $sortedList[$#sortedList] );
@cleanList;
} # lineSort
 
D

Dr.Ruud

Dave Weaver schreef:
Jim Gibson:

You have an off-by-one error.

for my $i ( 0 .. $#sortedList - 1 ) {

Your "You" is Katie?

$#ary is the index of the last element of @ary.
(normally equal to 1 less than the number of elements)

See perldsc, perlop.

perl -e '@_=(4,5,6); print $#_, "\n"'
perl -e '{local $[=1; @_=(4,5,6); print $#_, "\n"}'

Context-free coding: for my $i ( $[ .. $#sortedList ) {}

But see perlvar: the use of $[ is discouraged.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top