Tahmtan Ebrahimi
Tahmtan Ebrahimi

Reputation: 99

delete a key from a hash of array but I get Use of uninitialized value in hash element

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.

Upvotes: 1

Views: 189

Answers (2)

xxfelixxx
xxfelixxx

Reputation: 6602

The warnings are due to the fact that you are deleting elements of your hash while you are iterating over them, which is in general, a bad idea.

Perl is warning you that you are trying to print something that is no longer there. Also, be aware that the keys are returned in random order unless you sort them.

Take a look at:

How should I delete hash elements while iterating?

Upvotes: 1

stevesliva
stevesliva

Reputation: 5665

It looks like you are deleting 3009848 before the 3009848 key is iterated in the foreach loop. You can probably try a simple next unless defined $seq->{$s}; since you've deleted $s==3009848 on the interation for 3009824. defined checking never hurts...

Upvotes: 2

Related Questions