this is probably simple, but still I am stuck. How do you access a hash through its hash reference and test if part of it (a subhash) exists? What I would like to do is print individuals only if coordinate information is available for them. Information about the individuals is stored in one hash accessed through a hash reference, coordinate information in another hash accessed through a hash reference. This is the code:
use strict; use warnings; my $individuals = {}; my $coordinates = {}; # Fill the hashes associated with $individuals and $coordinates # in subroutines so that # $$coordinates{$group}{$id}{$stage}{"coords"}{$coord_no} = $value print_coords($individuals, $coordinates); sub print_coords { my ($individuals, $coordinates) = @_; foreach my $group (sort keys %$individuals) { foreach my $id (sort keys %{$$individuals{$group}}) { INDIVIDUAL: foreach my $stage (sort keys %{$$individuals{$group}{$id}} +) { # here the test if (!(%{$$coordinates{$group}{$id}{$stage}{"coords"})) + { next INDIVIDUAL; } # print individual and coordinate information } } } }
Running it results in: "Can't use string ("") as a HASH ref while "strict refs" in use at ...
I have been struggling with this for a while and would be very happy if you could help me with it. Henri
In reply to Test if a subhash in a referenced hash exists by Henri
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |