The problem is that you use the matching operator in scalar context instead of list context. In scalar context it would return the number of matches (0 or 1).
my $pat = $line =~ m/^LOC_Os0[1-7]g[0-9]*.[0-9]\s/;
should be
my ($pat) = $line =~ m/^LOC_Os0[1-7]g[0-9]*.[0-9]\s/;
I would write the script more like this in order to have error handling and avoid rewriting the whole file for each new entry.
#!/usr/local/bin/perl use strict; use warnings; use autodie; open (FILE, "<:utf8", "outputps_scan_chr1_.out"); my %seen = (); open (MYFILE, ">:utf8", "data.txt"); open (WASTE, ">:utf8", "waste.txt"); while (defined(my $line = <FILE>)) { my $pat; next if ($line !~ m/^(LOC_Os0[1-7]g[0-9]*.[0-9])\s/); $pat = $1; if (!$seen{$pat}++) { print MYFILE $line; } else { print WASTE $line; } } close (MYFILE); close (WASTE); close (FILE);
Update: I forgot to mention that I changed the pattern matching also.

First I want to know if there has been a match and then ignore the line, if there wasn't one.
Instead of matching a second time to get the pattern, I used a capture (...) in the pattern. Then I can retrieve the matched string in $1 and assign it to $pat.


In reply to Re: pattern search then remove duplicacy by hexcoder
in thread pattern search then remove duplicacy by Anonymous Monk

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.