I would like to know how to remove a key from a hash of array. Given the following file as a simple example, forth column is always the third column in the paired match and data in not sorted.
....
NS501570 WWW 3009824 3009848
....
NS501572 WAD 3009848 3009898
....
I would like to collapse it into
NS501570 WWW 3009824 3009848 NS501572 WAD 3009848 3009898
I wrote the following code. I selected third column as key for hash. therefore I print the values from the first entry and column 4 would be the key of the other one:
use warnings;
use strict;
my $seq;
while(<>){
chomp;
my @line = split;
$seq->{"$line[2]" } = [@line];
}
foreach my $s (keys %{$seq} ) {
### @{ $seq->{$s}}[3] key for the second pair
print @{ $seq->{$s}}[0],"\t",@{ $seq->{$s}}[1],"\t",@{ $seq->{$s}}[2],"\t",@{ $seq->{$s}}[3],"\t",@{ $seq->{@{ $seq->{$s}}[3]}}[0],"\t",@{ $seq->{@{ $seq->{$s}}[3]}}[1],"\t",@{ $seq->{@{ $seq->{$s}}[3]}}[2],"\t",@{ $seq->{@{ $seq->{$s}}[3]}}[3],"\n";
delete ${$seq}{@{ $seq->{$s}}[3]};
}
it works but there is a small problem! I do no know how to delete the second key (in this case 3009848 in the second entry) without getting the following error:
NS501570 WWW 3009824 3009848 NS501572 WAD 3009848 3009898
Use of uninitialized value in hash element at find_mate2.pl line 22, <> line 2.
Use of uninitialized value in hash element at find_mate2.pl line 22, <> line 2.
Use of uninitialized value in hash element at find_mate2.pl line 22, <> line 2.
Use of uninitialized value in hash element at find_mate2.pl line 22, <> line 2.
Use of uninitialized value in print at find_mate2.pl line 22, <> line 2.
Use of uninitialized value in print at find_mate2.pl line 22, <> line 2.
Use of uninitialized value in print at find_mate2.pl line 22, <> line 2.
Use of uninitialized value in print at find_mate2.pl line 22, <> line 2.
Use of uninitialized value in print at find_mate2.pl line 22, <> line 2.
Use of uninitialized value in print at find_mate2.pl line 22, <> line 2.
Use of uninitialized value in print at find_mate2.pl line 22, <> line 2.
Use of uninitialized value in print at find_mate2.pl line 22, <> line 2.
Use of uninitialized value in delete at find_mate2.pl line 23, <> line 2.
delete ${$seq}{@{ $seq->{$s}}[3]};is a pretty bizarre statement. Do you mean something like$key2 = $seq->{$s}[3]; delete $seq->{$key2};?@{ $seq->{$s}}[n]is better written as$seq->{$s}[n]