#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2014
    Posts
    17
    Rep Power
    0

    find duplicates and add them


    <p> 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 below. 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 with the sum of all the duplicates.


    the code looks like this. </p>

    <c>#!/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";

    }
    }






    }

    </c>

    Output: 725 Hong Kong Xingtong XT B10/B20/B30/B90
    725 Nokia 1280
    726 Nokia 1100
    726 Nokia 1280/Nokia 1282
    727 Nokia 1650
    727 Shenzhen ZA606
    728 Nokia 1209
    729 Quectel Wireless M10
    730 Dalian Daxian X901
    731 CEC CECT C2000
    731 Huawei E156/Huawei E156B/Huawei E156C/Huawei E156G (USB)
    731 Samsung E2222
    733 East King GEEPAS/GNEX/G4001
    733 HK Disike DISKO D600 Mini/S4 One/M7/S901e
    733 Nokia 1110i
    733 Nokia 1208/Nokia 1208b
    734 Master DXD D2
    735 Huawei E5251s-2
    735 Nokia 1112/Nokia 1112b/Nokia 1112c
    737 Alcatel 1010D
    737 Nokia 1280/Nokia 1282
    738 Allied CARLVO V6/A12/A8/A7
    738 HTC ARTE100/HTC P3300/HTC Artemis
    738 Nokia 1202
    739 Ningbo Bird K228/K103/K107/K228+
    739 Nokia 6030/Nokia 6030b
    740 Nokia 1110
    741 Motorola W220
    741 Tecno T511
    743 Tecno Itel IT1450
    745 Nokia 1280/Nokia 1282
    747 Shenzhen Tango ITEL C8000
    748 Huawei U1250
    749 Nokia 1200
    749 TRIDENT MBT6800
    750 Tecno T611S
    751 Gtide G220/G117/G009
    752 Panasonic FOMA P902i
    753 Nokia 2600 Classic
    755 Ningbo Bird V100
    755 Samsung E2222
    756 Nokia 1100
    758 Nokia 1280/Nokia 1282
    758 Tecno T340
    759 Nokia 1110i
    760 Gowin E88/G5/K5/9800
    760 Nokia 2600 Classic
    762 Nokia 1650
    762 ZTE E260
    765 Nokia Asha 210 Dual SIM
    767 Giga DUBAO V5/V6/V7/V9
    767 Nokia 1208/Nokia 1209
    767 Nokia 1600
    771 Motorola C115
    771 Shenzhen Tango ITEL C7000
    774 Master G60/G70/G80/G90
    775 Nokia 1110
    775 Tecno T340
    776 Nokia 1110
    776 Nokia 1202
    776 Shenzhen Tango ITEL IT868
    777 Hongtong Exactitude KZG G93A
    777 Nokia 1200
    777 Nokia 2626
    777 Nokia 5000/Nokia 5000d
    777 Tecno T35
    778 Nokia 1110
    779 Nokia 1202
    780 Tecno T551
    783 Gowin E9 E72I E83 E75
    786 Tecno Itel IT330
    787 HK Aardmen ADMET i99/F2/F8
    787 Nokia 1110
    788 Tecno T340
    789 Nokia 1650
    790 Nokia 1600
    791 Nokia 1208/Nokia 1209
    791 Nokia 1280/Nokia 1282
    793 Motorola C115
    793 Nokia 1112/Nokia 1112b/Nokia 1112c
    794 Nokia 1100
    794 Nokia 1280
    795 NEC e616/NEC c616
    795 Nokia 1600
    796 Shenzhen Sixbulls SL1000/S5130/F100/F200
    796 Tecno Itel IT3660
    797 Nokia 2600 Classic
    798 Source J-MAX Matrix II
    800 Nokia 1280/Nokia 1282
    801 Huawei E2010 (USB)
    801 Unis-Hitech T1368
    802 Nokia 1650
    803 HK Aardmen ADMET B11/B22/F6/F9
    803 Nokia 1110
    804 Nokia 1100
    805 GTIDE G12
    808 Ha Wah HX 1280i/X3/210/i7/M73/H1120
    810 AUX S20
    810 Nokia 1200
    811 Tecno Itel IT6200
    814 KT Tech TAKE KM-S220
    814 Tongxin G-BEST N8
    816 Nokia 1200
    816 Nokia 6030/Nokia 6030b
    818 Inventec G201
    820 Nokia 1200
    820 Nokia 1200
    821 Nokia 1112/Nokia 1112b/Nokia 1112c
    825 Nokia 1208/Nokia 1208b
    825 Tess GM9-18
    826 Nokia 1280/Nokia 1282
    831 Nokia 1200
    831 Nokia 1202
    831 Nokia 1600
    831 Shenzhen XiSheng CCIT N800
    832 Tecno T722
    833 Nokia 6030/Nokia 6030b
    833 Tecno T580
    839 Alcatel OT 305/Alcatel OT 305a
    839 Tecno Itel IT1350
    839 Tongxin G-BEST M66
    843 Nokia 1100
    846 Nokia 6070
    847 Hong Kong Jmini Mobile X5-00/J888/J333mini/J228
    849 Nokia 1200
    850 Shenzhen Aolixin MBO M71
    851 Nokia 1208/Nokia 1208b
    852 Tecno Itel IT320
    853 CECT C600
    855 Nokia 1208/Nokia 1208b
    858 Nokia 1110
    860 Smadl D95
    864 Dongguan Yong Jin Da G8000/G3-02/G3-05/G3-06/G3-08
    865 Nokia 2310
    865 Tecno T381
    866 Panasonic FOMA P905iTV
    869 Nokia 1600
    870 Nokia 1110i
    870 Nokia 1600
    872 Huawei G2800S
    873 Master G-Fen M500
    873 Nokia 1108
    873 Nokia 1200
    875 Shenzhen XiSheng CCIT 5360
    875 Tecno T4
    876 Nokia 1280/Nokia 1282
    880 Motorola F3
    881 Nokia 1202
    881 Nokia 1680 Classic
    882 Nokia 1100
    882 Tecno 7336
    886 Nokia 1100
    888 Nokia 1209
    889 Asian-Star X905
    889 Gright 6700/L600/M1/E72
    890 Tecno T511
    891 Nokia 6070
    894 Nokia 1200
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Location
    Paris area, France
    Posts
    846
    Rep Power
    500
    When you think unique values, think hash. Make a hash where the key is the model and the value the number of users.

    Assuming you have at one point the model and the number of users in the $model and $value variables, simply populate the hash this way:
    Code:
    $hash{$model} += $value;
    #
    If the hash entry does not exist yet, it will be created with the content of $value. If the entry already exists (i.e. this is a duplicate), it will be populated with the sum of $value and the current content of the hash for that key.

    At the end, the hash contains a unique list of models with the total number of users for each.

IMN logo majestic logo threadwatch logo seochat tools logo