dictionary - Perl: Search and Replace -


i'm trying improve script in hope match characters in input.txt (column 4: h1, 2hb, ca, hb3) dictionary.txt , replace appropriate characters dictionary.txt (column 2: h, hb, c, 3hb). using dictionary.txt dictionary:

input.txt

1  n  22  h1  met 1  h  32 2hb  met 1  c  40  ca  met 2  h  35  hb3 asp 

dictionary.txt

met  h   h1 met  hb 2hb met  c   ca asp 3hb  hb3 

output

1  n  22  h  met 1  h  32  hb met 1  c  40  c  met 2  h  35 3hb asp 

i'm trying approach first matching word in input.txt (met) , dictionary.txt (met) , performing substitution. i've written far:

#!/usr/bin/perl  use strict; use warnings;  %dictionary;  open $dic_fh, '<', 'dictionary.txt' or die "can't open file: $!";  while (my $ref = <$dic_fh>) {     chomp $ref;     @columns  = split(/\t/, $ref);     $res_name = $columns[0];     $ref_nuc  = $columns[1];     $dictionary{$res_name} = {$ref_nuc};      open $in_fh, '<', 'input.txt' or die "can't open file: $!";      while (my $line = <$in_fh>) {         chomp $line;         @columns = split(/\t/, $line);         @name = $columns[3];         if (my $name eq $res_name) {             $line = $_;             foreach $res_name (keys %dictionary) {                 $line =~ s/$name/$dictionary{$ref_nuc}/;             }             print $line;         }     } } 

the problem seems assigning single field $columns[3] array @name, , expecting find in $name, separate variable altogether. declare $name @ point of comparison

you executing statement

$line =~ s/$name/$dictionary{$ref_nuc}/; 

once each key in hash. unnecessary: needs done once. better change value of $columns[3] $dictionary{$columns[3]} instead of doing search , replace on whole line, target string may appear in other columns don't want modify

it simple building dictionary hash , replacing fourth field of input file dictionary lookup

use strict; use warnings; use 5.010; use autodie;  open $fh, '<', 'dictionary.txt'; %dict; while ( <$fh> ) {   ($k, $v) = (split)[2,1];   $dict{$k} = $v; }  open $fh, '<', 'input.txt'; while ( <$fh> ) {   @fields = split;   $fields[3] = $dict{$fields[3]};   join "\t", @fields; } 

output

1   n   22  h   met 1   h   32  hb  met 1   c   40  c   met 2   h   35  3hb asp 

Comments

Popular posts from this blog

javascript - Bootstrap Popover: iOS Safari strange behaviour -

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

session - Logging Out Using PHP -