baxy77bax has asked for the wisdom of the Perl Monks concerning the following question:

hi,

i'll just skip to the point:

this is my hash

$VAR1 = { '0000006' => [ 47, 986, { '6' => [ undef, 8, '0.0476673428', '0.0000000000', '0.0000000000', '0.0504833512', '-2.3718998111e+01', '6.7559883050e-01', '0.0000000000e+00', '0.0000000000e+00' ], '4' => [ undef, 3, '0.0476673428', '0.0000000000', '0.0000000000', '0.0502136752', '-2.3718998111e+01', '8.6357443606e-01', '0.0000000000e+00', '0.0000000000e+00' ], '1' => [ 19, 439, '0.0476673428', '0.0432801822', '0.0452380952', '0.0539499037', '-1.7611636075e-01', '3.3572455230e-01', '2.3371924011e-01', '4.6743848022e-01' ], '3' => [ 'NA' ], '2' => [ 22, 286, '0.0476673428', '0.0769230769', '0.0833333333', '0.0370370370', '8.1093021684e-01', '9.9753809371e-01', '9.9388971159e-01', '1.0000000000e+00' ], '5' => [ 6, 249, '0.0476673428', '0.0240963855', '0.0246913580', '0.0589080460', '-8.6952438185e-01', '2.6721209055e-02', '9.8480960134e-03', '1.9696192027e-02' ] } ] }
and what i need to do is loop through $hash{$_}[2]{$_} (1..6) and evaluate if there is something on 9th position. something like:
@arg{key} = (1 .. 6); foreach my $physh (@{$arg{key}}){ next if (!$hash{$_}[2]{$physh}[9]); ... }
the problem is when i run this foreach loop the result i get is:
$VAR1 = { '' => [ undef, undef, { '' => [] } ], 'GO:0000006' => [ 47, 986, { '6' => [ undef, 8, '0.0476673428', '0.0000000000', '0.0000000000', '0.0504833512', '-2.3718998111e+01', '6.7559883050e-01', '0.0000000000e+00', '0.0000000000e+00' ... the: '' => [ undef, undef, { '' => [] } ], is something that i don't need- it creates a problem further in the co +de
the empty location is created whenever there is nothing on 9th position. also it has to be checked for 9th position because sometimes 'NA' doesn't mean that there is nothing on 9th position.

any ideas ??

also i'm using warnings so $hash{$_}[2]{$_} is warning me a lot , any way around it except local $^W = 0

Update:

in other words i have to clean up after myself !? :)

Replies are listed 'Best First'.
Re: searching through hash values
by almut (Canon) on Apr 08, 2009 at 11:59 UTC
    the empty location is created whenever there is nothing on 9th position.

    Search for "autovivification", e.g. on this site.

Re: searching through hash values
by kyle (Abbot) on Apr 08, 2009 at 12:37 UTC
Re: searching through hash values
by jethro (Monsignor) on Apr 08, 2009 at 12:24 UTC
    > perl -MData::Dumper -e ' @arg{key} = (1 .. 6); print Dumper(\%arg);' $VAR1 = { 'key' => 1 };

    Doesn't look quite right. An array (and you want an array, not a hash) with the elements 1..6 would be created with

    my @arg= 1..6;
    Also you should check whether an element exists in a hash before you try to access it, if you don't want to create it through Autovivication

    if (exists $hash{$_}) { foreach my $physh (sort keys %{$hash{$_}[2]}){ next if (!$hash{$_}[2]{$physh}[9]); ... }

    Note that I just loop through all keys of the subhash and therefore don't need the array with numbers 1..6. That doesn't work if there are other keys in that subhash you don't want to test. In that case just use 1..6 instead of sort keys %{$hash{$_}[2]}

    If you don't mind the sequence in which the hash elements are checked you can drop the sort.