user3616128
user3616128

Reputation: 377

Coutn unique occurance of match in a line

I have file with entry: (----) Manish Garg 74163: V2.0.1_I3_SIT: KeyStroke Logger decrypted file for key stroke displayed a difference of 4 hours from CCM time. - 74163: KeyStroke Logger decrypted file for key stroke displayed a difference of 4 hours from CCM time. 2014/07/04 I want to look for the unique count of id "74163" or any id in a line. Currently it gives the output as : updated_workitem value> "74163" Count> "2" But i want the count value as 1.(I dont want to include duplicate entry in count)

My code is 
my $workitem;
$file = new IO::File;
$file->open("<compare.log") or die "Cannot open compare.log";
@file_list = <$file>;
$file->close;
foreach $line (@file_list) {

        while ($line =~ m/(\d{4,}[,|:])/g ){
        @temp = split(/[:|,]/, $1);
        push @work_items, $temp[0];
                                        }
                                }

my %count;
my @wi_to_built;
map { $count{$_}++ } @work_items;

foreach $workitem (sort keys (%count)) {
chomp($workitem);
print "updated_workitem value> \"$workitem\"\n";
print "Count> \"$count{$workitem}\"\n";
}

Upvotes: 0

Views: 37

Answers (2)

RobEarl
RobEarl

Reputation: 7912

Use a hash to track unique ids found in a particular line:

foreach my $line (@file_list) {
    my %line_ids;
    while ($line =~ m/(\d{4,})[,|:]/g ){
        $line_ids{$1} = 1; # Record unique ids
    }
    push @work_items, keys %line_ids; # Save the ids
}

Note, I've changed your regex slightly so you don't need to split to a temporary array.

Upvotes: 1

Chankey Pathak
Chankey Pathak

Reputation: 21666

You can remove duplicate from array before doing map { $count{$_}++ } @work_items;

@work_items = uniq(@work_items);

sub uniq {
    my %seen;
    grep !$seen{$_}++, @_;
}

Demo

Upvotes: 0

Related Questions