Simple demo of my confusion follows. I have lines in an array. Trying to create a new array of uniq lines.

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.