vendredi 31 août 2018

Perl if statement and hash

My script prints multiple copies of the $value in the hash because there is likely a problem with the way my if statement is written. It is printing the entire hash for each occurrence where there is a regex match.

How do I restructure my if statement such that for each match only the corresponding value is printed and not the entire array?

Example Input

1 .....  A
2 .....  A
3 .....  WT00
4 .....  WT00

Example Output

3
4
3
4

Desired Output

3
4

Script

 #!/usr/bin/perl
 use strict;
 use autodie;
 use File::Basename;

 my $input_path = $ARGV[0];
 my %hash;
 my $key;
 my $value;

 open(my $input_fh, "<", $filename) or die $!;
 while (my $data = < $input_fh > ) {
     chomp $data;
     my@ columns = split '\s+', $data;
     my($firstletter) = ($columns[2] =~ m/^(\w)/);
     my($coordinates) = sprintf("%9.5f%16.5f%16.5f\n", $columns[5], $columns[6], $columns[7]);
     $key = $columns[10];
     $value = $columns[1];
     $hash {$key} = $value;
     if ($key =~ m/WT00/){
         print "$hash{$key}\n";
     }
}

Aucun commentaire:

Enregistrer un commentaire