Home: Need a Custom or Prewritten Perl Program?: I need a program that...:
Check for duplicates and add values



biboshakan
New User

Jun 12, 2014, 2:19 AM


Views: 20535
Check for duplicates and add values

Hey everyone how's it going? I'm fairly new to perl so what i'm gonna ask might seem basic, dont judge :P

I wrote a code that gives as an output phone models in a country with the number of users using the model, I have attached the output here. As you may notice, there are duplicates of phone models and what I would like to do is add the values of these duplicates in order to have a unique model


the code looks like this.

Code
#!/usr/bin/perl 


use strict;
use warnings;
use diagnostics;

open (IMEI, 'IMEI.txt');
open (TAC, 'tac.txt');
my %mapToModel;
my %keyCount;
my $key;
my $model;
my $subs;
my $keyCount;
my $mapToModel;
my $count;
my %dictionary;

while (<TAC>){
$key = substr $_,0,8;
$model = substr $_,9;
$keyCount{$key} = 0;
$mapToModel{$key} = $model;
}


close (TAC);

while (<IMEI>){
$subs = substr $_, 0, 8;
if(exists $keyCount{$subs}){
$keyCount{$subs} = $keyCount{$subs} +1;
}
}
close (IMEI);
foreach my $key (keys %keyCount){
if (exists $mapToModel{$key}){


$model = $mapToModel{$key};
$count = $keyCount{$key};

foreach my$model (keys )


if( $count !=0){

print "$count\t$model";

}
}






}


any ideas on how to do that??
Attachments: blalbla.txt (16.9 KB)


Zhris
Enthusiast

Jun 13, 2014, 2:26 PM


Views: 19200
Re: [biboshakan] Check for duplicates and add values

Hi,

Your code could likely be optimised to produce the end result more efficiently, but for simplicity I propose adding another level of data crunching, a hash where its keys are the phone model and its values are a running total of users.

Declare the hash at the top with your other variable declarations:

Code
my %output;


Instead of printing, construct the proposed hash. If the model already exists in the hash, it will add the current count to the existing count:

Code
print "$count\t$model"; 
$output{$model} += $count;


Output the hash at the end. Usually you would have to print a newline character on the end, but since you didn't chomp it off the model earlier, its not necessary:

Code
print "$output{$_}\t$_" for ( keys %output );


If you want to order the data highest to lowest number of users you could use the below instead:

Code
print "$output{$_}\t$_" for ( sort { $output{$b} <=> $output{$a} } keys %output );


If you provide us with example input data (IMEI.txt and tac.txt), it will be easier for us to test your code and provide further suggestions.

Chris


(This post was edited by Zhris on Jun 13, 2014, 2:32 PM)


Laurent_R
Veteran / Moderator

Jun 14, 2014, 1:19 AM


Views: 18792
Re: [biboshakan] Check for duplicates and add values

When you cross-post questions on various places, please inform the reader, so as to avoid duplicates work on various part of the Internet. In this case I had already provided an answer two days ago on the Dev Shed forum (http://forums.devshed.com/perl-programming-6/duplicates-add-962160.html).


(This post was edited by Laurent_R on Jun 14, 2014, 1:24 AM)


biboshakan
New User

Jun 18, 2014, 3:59 AM


Views: 14786
Re: [Laurent_R] Check for duplicates and add values

Okay Laurent, will do, thanks for the help :)