CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

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


New User

Jun 12, 2014, 2:19 AM

Post #1 of 4 (20687 views)
Check for duplicates and add values Can't Post

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.


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)


Jun 13, 2014, 2:26 PM

Post #2 of 4 (19352 views)
Re: [biboshakan] Check for duplicates and add values [In reply to] Can't Post


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:

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:

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:

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:

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.


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

Veteran / Moderator

Jun 14, 2014, 1:19 AM

Post #3 of 4 (18944 views)
Re: [biboshakan] Check for duplicates and add values [In reply to] Can't Post

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 (

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

New User

Jun 18, 2014, 3:59 AM

Post #4 of 4 (14938 views)
Re: [Laurent_R] Check for duplicates and add values [In reply to] Can't Post

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


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives