I have written a code to access a nested data (very complicated) data structure in perl.
<%method searchFeatureInFG>
<%args>
$featureGroup
$featureNameHash
</%args>
<%init>
my $foundFeature = 0;
my $foundGroup = undef;
my $foundIndex = undef;
if(defined $featureGroup && defined $featureNameHash){
for(my $i = 0; $i < @$featureGroup; $i++){
#print "<pre>".Data::Dumper::Dumper($featureGroup->[$i]->{'features'})."</pre>";
if(ref($featureGroup->[$i]) eq 'HASH'){
if(defined $featureNameHash->{$featureGroup->[$i]->{'name'}}){
$foundGroup = $featureGroup;
$foundIndex = $i;
$foundFeature = 1;
}
elsif(defined $featureGroup->[$i]->{'features'}){
for(my $j = 0; $j<scalar @{$featureGroup->[$i]->{'features'}} ; $j++){
for(my $k=0;$k<scalar @{$featureGroup->[$i]->{'features'}->[$j]->{'features'}};$k++) {
if (defined $featureGroup->[$i]->{'features'}->[$j]->{'features'}->[$k]->{'name'}) {
print $featureGroup->[$i]->{'features'}->[$j]->{'features'}->[$k]->{'name'}."\n";
}
}
last if !defined $featureGroup->[$i+1]->{'features'};
}
}
}
}elsif(defined $featureNameHash->{$featureGroup->[$i]}){
$foundFeature = 1;
$foundGroup = $featureGroup;
$foundIndex = $i;
}
last if($foundFeature);
}
}
return ($foundFeature, $foundGroup, $foundIndex);
</%init>
</%method>
This gives me errors like the following.
Global symbol "$i" requires explicit package name at
Global symbol "$foundFeature" requires explicit package name at
Global symbol "$foundGroup" requires explicit package name at
Global symbol "$featureGroup" requires explicit package name at
Global symbol "$foundIndex" requires explicit package name at
What does these errors mean ?
You are getting these errors because of scoping issues. Take a look at the first one
Global symbol "$i" requires explicit package name at
You initially define $i
in the line
for(my $i = 0; $i < @$featureGroup; $i++){
But then later try to use it outside of that for loop. Perl is giving you these errors (probably because you are using strict like you should) because $i
is only defined inside that for loop.
To fix this move those variable definitions outside of any scoped loops, i.e. immediately after <%init>
declare
my $i
my $foundFeature
...