perl-diddler has asked for the wisdom of the Perl Monks concerning the following question:
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;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Braino - why is this not working?
by kcott (Archbishop) on Oct 18, 2010 at 22:37 UTC | |
by perl-diddler (Chaplain) on Oct 19, 2010 at 01:04 UTC | |
by kcott (Archbishop) on Oct 19, 2010 at 17:04 UTC | |
|
Re: Braino - why is this not working?
by duelafn (Parson) on Oct 18, 2010 at 22:46 UTC | |
by perl-diddler (Chaplain) on Oct 19, 2010 at 00:58 UTC | |
by Anonymous Monk on Oct 19, 2010 at 01:22 UTC | |
by perl-diddler (Chaplain) on Oct 19, 2010 at 03:35 UTC | |
by perl-diddler (Chaplain) on Oct 19, 2010 at 04:35 UTC | |
|
Re: Braino - why is this not working?
by suhailck (Friar) on Oct 19, 2010 at 01:29 UTC | |
by perl-diddler (Chaplain) on Oct 19, 2010 at 04:22 UTC | |
|
Re: Braino - why is this not working?
by aquarium (Curate) on Oct 18, 2010 at 23:20 UTC | |
by perl-diddler (Chaplain) on Oct 19, 2010 at 01:11 UTC | |
|
Re: Braino - why is this not working?
by jacuro (Initiate) on Oct 20, 2010 at 01:58 UTC |