I first deblank and lines goes from 4->3. Then I try uniq, and it goes down to '1', but the lines aren't uniq, instead, they are in '@data' and I don't see why each line is matching in the grep after the first one is pushed on the 'answer' array. I.e. every grep after the first claims that each successive line (all differing) is found in the 'results array', (which after the first match contains the first line). I even print the matching line, and the results array, and they are different, but the claim is that there is a 'match'. Why? What am I missing? Sigh. Thanks...
#!/usr/bin/perl -w use strict; use feature ':5.10'; my @data=( "abcdefg\n", "hijklmno\n", "pqrstuvw\n", "\n"); my $start_lines; sub show ($\@) { $start_lines or return; my ($when,$lines)=($_[0],1+$#{$_[1]}); my $diff = $start_lines - $lines; my $prc = $diff ? 100.0*$diff/$start_lines : 0; printf "%10.10s %d -> %d lines or %.2f%% reduction\n", $when, $start_lines, $lines, $prc; } sub printar(\@) { my $ar=$_[0]; printf "\n(#lines=%d,", 0+@$ar; for (my $i=0; $i<=$#$ar; ++$i) { printf " line %d: '%s'", $i, $ar->[$i]; } print ")\n"; } sub uniq(\@) { my $ar=$_[0]; my @res; foreach (@{$ar}) { my $ans=grep /^$_$/, @res; printf "line \"%s\" in ar?: %s, ar: ", $_ // "undef" , $ans?"yes":"no"; printar @res; if (!$ans) {push @res, $_} } @res; } ### main #my @msgs=<>; my @msgs=@data; $start_lines=@msgs; show("before", @msgs); my $msgs=join "", @msgs; my @deblanked=split /\n/, $msgs; show "deblanked", @deblanked; my @uniq=uniq @deblanked; show "after uniq", @uniq;
In reply to Braino - why is this not working? by perl-diddler
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |