perl_mystery has asked for the wisdom of the Perl Monks concerning the following question:

I want to avoid printing duplicates.I have three variables in a print statement like below.For example if the print statment prints " 1 2 3",this pattern should not be printed.Can someone pls suggest how to do it?

print $line, $match, $link, "\n";

If the following pattern gets printed,it should not get printed again

1 2 3

Replies are listed 'Best First'.
Re: How to avoid printing duplicates
by Ratazong (Monsignor) on Dec 04, 2010 at 19:52 UTC

    When you think duplicate, think hash.

    untested:

    my %patterns; if (!($patterns{"$line, $match, $link"})) # if not yet in the has +h { print $line, $match, $link, "\n"; # print it $patterns{"$line, $match, $link"} = 1; # ... and add it to the + hash }
    HTH, Rata
      I think he meant each variable may be a duplicate of the next, in which case it would be slightly more complex, but that principle (hashes) still does apply.
Re: How to avoid printing duplicates
by TomDLux (Vicar) on Dec 06, 2010 at 05:14 UTC

    You present this over-simplified problem, How do I keep from printing duplicates?, but you say nothing about where the data is coming from.

    The best solution is to avoid generating duplicates, if you can.

    As Occam said: Entia non sunt multiplicanda praeter necessitatem.