1

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.
2
  • delete ${$seq}{@{ $seq->{$s}}[3]}; is a pretty bizarre statement. Do you mean something like $key2 = $seq->{$s}[3]; delete $seq->{$key2}; ? Commented Apr 28, 2015 at 2:45
  • And for that matter, @{ $seq->{$s}}[n] is better written as $seq->{$s}[n] Commented Apr 28, 2015 at 2:46

2 Answers 2

2

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...

Sign up to request clarification or add additional context in comments.

Comments

1

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.