Home > Archive > PERL Miscellaneous > March 2006 > sort and keep the latest version
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
sort and keep the latest version
|
|
|
| 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
------------------------------------------------------------------------------------------------------------------------------------
What are those $l1_f2, $l2_f2, $l1_f3 ... mean?
Thanks,
| |
| Jim Gibson 2006-03-28, 9:59 pm |
| In article <1143594003.968550.148330@z34g2000cwc.googlegroups.com>,
Katie <hoa19ngo@gmail.com> wrote:
> 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?
This looks like a very old subroutine. Suggested changes are noted
below.
>
> -------------------------------------------------------------------------------
> -----------------------------------------------
use strict;
use warnings;
> sub lineSort {
> ($mode, @list) = @_;
my( $mode, @list ) = @_;
> local (@sortedList, @cleanList);
my( @sortedList, @cleanList );
> @sortedList = sort( @list );
>
> for ( $i = 0; $i < $#sortedList; $i++ ) {
for my $i ( 0 .. $#sortedList ) {
> ($l1_f1, $l1_f2, $l1_f3, $l1_f4) = split( /[ \t]+/, $sortedList[$i]);
my ( $l1_f1, ...
> ($l2_f1, $l2_f2, $l2_f3, $l2_f4) = split( /[ \t]+/,
> $sortedList[$i+1]);
(ditto, except there will be no text when $i is equal to
$#sortedList, the greatest index of the array @sortedList, so $l2_f1
will all be undefined -- maybe that is OK)
> 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] );
To save the first line of @sortedList instead of the last line, replace
the above with:
push( @cleanList, $sortedList[0] );
whether or not that has the intended effect is to be determined (by
you).
> @cleanList;
> } # lineSort
>
> -------------------------------------------------------------------------------
> -----------------------------------------------------
>
> What are those $l1_f2, $l2_f2, $l1_f3 ... mean?
Those are scalar variables. They are assigned the values that result
from the split operator acting on the contents of $sortedList[$i],
which will split the text on spaces or tabs, thanks to the /[ \t]+/
regular expression. The split function returns a list, and the values
from that list are copied to $l1_f2, $l2_f2, etc.
--
Jim Gibson
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
| |
| Dave Weaver 2006-03-31, 4:00 am |
| Jim Gibson <jgibson@mail.arc.nasa.gov> wrote:
> Katie <hoa19ngo@gmail.com> wrote:
>
> for my $i ( 0 .. $#sortedList ) {
You have an off-by-one error.
for my $i ( 0 .. $#sortedList - 1 ) {
| |
| Dr.Ruud 2006-03-31, 6:59 pm |
| Dave Weaver schreef:
> Jim Gibson:
[color=darkred]
>
> 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.
--
Affijn, Ruud
"Gewoon is een tijger."
| |
| Jim Gibson 2006-03-31, 6:59 pm |
| In article <442cf240$0$4996$db0fefd9@news.zen.co.uk>, Dave Weaver
<zen13097@zen.co.uk> wrote:
> Jim Gibson <jgibson@mail.arc.nasa.gov> wrote:
>
> You have an off-by-one error.
>
> for my $i ( 0 .. $#sortedList - 1 ) {
Right, thanks. I missed that one. Therefore, my comment about going
past the end of the array is retracted with apologies.
| |
| Dr.Ruud 2006-03-31, 6:59 pm |
| Dr.Ruud schreef:
> Dave Weaver schreef:
>
> Your "You" is Katie?
Apparently not, there is a "[$i+1]" in the code.
--
Affijn, Ruud
"Gewoon is een tijger."
|
|
|
|
|