in reply to Re: Re: Killing dupes
in thread Killing dupes
First, this loop would never actually end; You test $y, which starts at 0 against the $vallid which is the size of the array @out. You increment $y outside the while loop, therefor it never gets incremented inside the loop. You could rewrite this as:@out = grep($_ ne $prev && ($prev = $_), @content); $vallid=@out; $y=0; open("LIST", ">$file_vol.flt") or die "Can't Open File: $! +"; while($y<=$vallid){ print LIST "$out[$y]"; } $y++; }
Notice that you do not need to set $vallid or $y and can avoid dealing with any kind of incrementation. Since you are however not really doing much inside the loop you could take advantage of how Perl deals with printing arrays and simply put:@out = grep($_ ne $prev && ($prev = $_), @content); foreach my $line (@out) { print $line; }
And finally at the risk of getting a little compressed you can get rid of @out and @content:@out = grep($_ ne $prev && ($prev = $_), @content); print @out;
This can be done because print can use the array returned from grep without having to store it into an array first. Also, grep can read directly from the file for you.open (DB,"$file_vol") or die; print grep($_ ne $prev && ($prev = $_), <DB>); close(DB);
|
|---|