Input file for this code:
test_data
#!/usr/bin/perl # Takes a three-column, tab-delimited file and creates two hashes. # The keys for both hashes will be the items in the first column, # and the values will be the second and third column ############################################################### use warnings; use strict; my $infile = shift @ARGV; my %institute; # Hash for the 2nd column data my %mutation; # Hash for the 3rd column data open(IN,'<', $infile) or die "Can't open file $infile: $!\n"; while(my $line = <IN>) { chomp $line; # The following line checks for a header line, # which in this case begins with "Sample" # Header lines can start with anything though, # so it's really important to verify your input data first next if($line =~ /Sample/); # The split() function returns an array, so this next line # puts each item in the returned array into three variables my ($sample, $inst, $mut) = split(/\t/,$line); # Then just assign the values to their respective hashes $institute{$sample} = $inst; $mutation{$sample} = $mut; } close(IN); ##Output # Print columns in reversed order print "Mut\tInst\tSample\n"; # print out a header foreach my $key (sort keys %institute) { my $ins = $institute{$key}; my $mu = $mutation{$key}; print "$mu\t$ins\t$key\n"; } |
Comments are closed.