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

I have lines as follows in a file

Today is amazing.nice day Today is amazing.nice but still I haven't done anything Yesterday was just a normal day Tomorrow probably will be amazing.new day

What i want to accomplish : I want to be able to go through lines and match "(amazing.*)". If (amazing.*) is unique then put it in an array and also print that line if not ignore it. Basically, this is the result I am looking for.

Today is amazing.nice day Tomorrow probably will be amazing.new day
and so forth....

So far I have only written this much code. I just don't know where to go from here. Any direction on how to approach this would be very much appreciated.

#!/usr/bin/perl -w use strict; while ( my $line = <> ) { if ( $line =~ m/(\(amazing.+?\))/) { my $string="$1"; @allstrings = ($string); } }
  • Comment on How to ignore lines that match certain string after it is matched for the first time within a same file
  • Select or Download Code

Replies are listed 'Best First'.
Re: How to ignore lines that match certain string after it is matched for the first time within a same file
by Kenosis (Priest) on Sep 19, 2012 at 23:47 UTC

    Perhaps the following will work for you:

    use strict; use warnings; my ( @array, %seen ); while ( my $line = <DATA> ) { if ( $line =~ /(amazing\.\S+)/ and not $seen{ lc $1 }++ ) { push @array, $line; #print $line; } } print for @array; __DATA__ Today is amazing.nice day Today is amazing.nice but still I haven't done anything Yesterday was just a normal day Tomorrow probably will be amazing.new day

    Output:

    Today is amazing.nice day Tomorrow probably will be amazing.new day

    $1 captures amazing\.\S+ and using lc on the hash key helps insure case is not significant in uniqueness.

    Hope this helps!

Re: How to ignore lines that match certain string after it is matched for the first time within a same file
by ikegami (Patriarch) on Sep 19, 2012 at 23:09 UTC
    my %seen; s/(amazing\S+)/ $seen{$1}++ ? ...new... : $1 /eg;