Assuming the order of the occurrences doesn't matter, the easiest way would be to store the digits as keys in a hash. Keys are always unique. You could use any value as your hash-value;
$file = "project.txt";
open(FILE,"$file");
my %hash = ();
while(my $a=<FILE>) {
if($a=~/\tCM+(\d*)/io) { print "$1\n"; $hash{$1}=undef; }
}
close FILE;
open(OUTPUT,">> project.out");
while(my $key = each %hash) {
print OUTPUT "$key\n";
}
close OUTPUT;
Or how I would probably write it:
my %done=();
open INPUT, "<$inputfile";
open OUTPUT, ">>$outputfile";
while(<INPUT>) {
next unless /\tCM+(\d+)/io;
next if exists $done{$1};
print OUTPUT, "$1\n";
$done{$1}=undef;
)
close INPUT;
close OUTPUT;
Haven't tested it, but it should work, and be fast... The <HANDLE> operator and regular expressions have the useful property that they both use the default variable ($_) if you supply none. This way you can bypass the use of $a.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.