Perl find duplicates in first column and convert to unique auto incremented value -


i have tab delimited text file (test.txt) looks following:

steve   ran 100 200 300 steve   sit 50  30  20 steve   steal   40  60  70 bill    ran 10  20  90 bill    14  15  30 john     34  38  29 john    ran 10  40  60 john    down    60  70  80 john    yep 40  69  80 

i need replace duplicate values in column 1 unique identifier, i.e. steve => name_1, bill => name_2, john => name_3, etc. order of text file important, read line line? here's have far...

use strict; use warnings; use autodie;  open $fh, "<", 'test.txt'; while (<$fh>) {     @row = split(/\s+/,$_);     print "$row[0]\t$row[1]\t$row[2]\t$row[3]\t$row[4]\n";       }  close $fh; exit; 

my desired output be:

name_1   ran 100 200 300 name_1   sit 50  30  20 name_1   steal   40  60  70 name_2   ran 10  20  90 name_2   14  15  30 name_3    34  38  29 name_3   ran 10  40  60 name_3   down    60  70  80 name_3   yep 40  69  80 

whenever duplicates need removed, using hash approach.

in case, need declare hash outside while loop, check each name see if exists in hash. if does, use value has, otherwise create new key, , store value want.

something like:

use strict; use warnings; use autodie;  open $fh, "<", 'test.txt'; %names; $count; while (<$fh>) {     @row = split(/\s+/,$_);     if (not exists $names{$row[0]}) {         $names{$row[0]} = "name_" . ++$count;     }     print "$names{$row[0]}\t$row[1]\t$row[2]\t$row[3]\t$row[4]\n";   }  close $fh; exit; 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -