should bemy $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.my ($pat) = $line =~ m/^LOC_Os0[1-7]g[0-9]*.[0-9]\s/;
Update: I forgot to mention that I changed the pattern matching also.#!/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);
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |