How can I set a $counter++
to hash unless is 1
? in this code:
use strict;
use warnings;
my @filestwo = glob("*.xml");
my $result = @filestwo;
my $count = 0;
my %justone;
foreach my $domain (@filestwo) {
open my $in, '<', $domain or die "Open fail on $domain $!
";
my @linestwo = <$in>;
for my $line (@linestwo) {
if($line =~ /Domain:s([a-z].+)/){
$count++;
print "Number:$count Your TLD $1!
" unless $justone{$1}++;
}
}
}
The output:
Number:1 Your TLD one.com!
Number:3 Your TLD three.com!
Number:5 Your TLD two.com!
Should be:
Number:1 Your TLD one.com!
Number:2 Your TLD three.com!
Number:3 Your TLD two.com!
Explanation about the code:
- Open 3 files
.xml
(In every file exist duplicate pattern) - Create a hash
%justone;
- Search from the array if the match the pattern: domain
- If match and in the next iteration match again the same pattern, just print 1 with
unless $justone{$1}++;
My problem, I can't set a counter just for the match relation unless hash.
See Question&Answers more detail:os