scope trouble

S

Shane

This module has scope issues, but I dont understand how
sub fork_guess {
my $puzzle = shift;
my $element_counter;
my %counted_hash;
my @array_keys;
my ($a, $b);
#Step 1
#loop through everything and find the first key with the lowest value
#ie, the first one with 2 values, or the first with 3
foreach my $listspot(@row_list){
foreach my $element(@{$listspot}){
if ( ref ($puzzle->{ $element }) eq "ARRAY") {
for ($counter=0;$counter <=8;$counter++){
if (defined $puzzle->{$element}[$counter]){$element_counter++}
}
line 234 %counted_hash{$element} = $element_counter;
}
}
}
238 @array_keys = sort {%counted_hash{$a} cmp %counted_hash{$b}}
keys %counted_hash;
print "ARRAY OF KEYS @arra_keys ##";
}


__EOF__
and these are the errors
syntax error at ./attempt3.pl line 234, near "%counted_hash{"
Global symbol "@array_keys" requires explicit package name at ./attempt3.pl
line 238.
Global symbol "%counted_hash" requires explicit package name
at ./attempt3.pl line 238.
syntax error at ./attempt3.pl line 238, near "%counted_hash{"
Global symbol "%counted_hash" requires explicit package name
at ./attempt3.pl line 238.
Execution of ./attempt3.pl aborted due to compilation errors.
 
S

Sherm Pendley

Shane said:
This module has scope issues, but I dont understand how

You should always fix errors one at a time in the order in which they appear,
and keep in mind that the line numbers reported by Perl are the lines on which
it *detects* an error, which is not always the same line (or even in the same
file) that actually *causes* the error.

Also, error messages after the first aren't 100% reliable; the interpreter
will try to recover and make what it sense it can of the code after an error,
but it can't always do so.

In this case:
for ($counter=0;$counter <=8;$counter++){
if (defined $puzzle->{$element}[$counter]){$element_counter++}
}

There's no opening bracket on that if(). The other errors are likely to be the
result of failed error recovery after the first error.

sherm--
 
X

xhoster

Shane said:
This module has scope issues, but I dont understand how
sub fork_guess {
my $puzzle = shift;
my $element_counter;
my %counted_hash;
my @array_keys;
my ($a, $b);
#Step 1
#loop through everything and find the first key with the lowest
value #ie, the first one with 2 values, or the first with 3
foreach my $listspot(@row_list){
foreach my $element(@{$listspot}){
if ( ref ($puzzle->{ $element }) eq "ARRAY") {
for ($counter=0;$counter <=8;$counter++){
if (defined
$puzzle->{$element}[$counter]){$e
lement_counter++} }
line 234 %counted_hash{$element} =
$element_counter;
}
}
}
238 @array_keys = sort {%counted_hash{$a} cmp %counted_hash{$b}}
keys %counted_hash;
print "ARRAY OF KEYS @arra_keys ##";
}

__EOF__
and these are the errors
syntax error at ./attempt3.pl line 234, near "%counted_hash{"

You have a syntax error at line 234, near "%counted_hash{". You should
use $, rather than %, when refering to a specific member of a hash.

Global symbol "@array_keys" requires explicit package name at
./attempt3.pl line 238.

The syntax error has caused perl to lose track of its scopes. If you
fix the syntax error, the scope will take care of itself.

Xho
 
S

Shane

You have a syntax error at line 234, near "%counted_hash{". You should
use $, rather than %, when refering to a specific member of a hash.



The syntax error has caused perl to lose track of its scopes. If you
fix the syntax error, the scope will take care of itself.

Xho

Indeed perl wanted all the %counted_hash to be $counted_hash (except for the
declaration)

Now the error is
Can't use "my $a" in sort comparison at ./attempt3.pl line 237.

Im trying to sort the keys by value, with a view of later taking the key
with the lowest value and doing things to it.
Am I going about this the right way?
 
X

xhoster

Shane said:
Indeed perl wanted all the %counted_hash to be $counted_hash (except for
the declaration)

Now the error is
Can't use "my $a" in sort comparison at ./attempt3.pl line 237.

That one is pretty self explanatory. If you need further help,
try adding "use diagnostics" to the top of the script (or just reading
perldoc perldiag. I get:

Can't use "my $a" in sort comparison at foo line 22 (#1)
(F) The global variables $a and $b are reserved for sort comparisons.
You mentioned $a or $b in the same line as the <=> or cmp operator,
and the variable had earlier been declared as a lexical variable.
Either qualify the sort variable with the package name, or rename the
lexical variable.

In your case, you can simply remove the spurious my ($a,$b)

Xho
 
T

Tad McClellan

Shane said:
Indeed perl wanted all the %counted_hash to be $counted_hash


No it didn't.

It wanted all %counted_hash{key} to be $counted_hash{key}.

The curly braces are part of the expression that perl is
trying to evaluate.

Now the error is
Can't use "my $a" in sort comparison at ./attempt3.pl line 237.


The solution to that would be to not use "my $a" in sort
comparison at ./attempt3.pl line 237.

Am I going about this the right way?


No, but explaining a better way would surely be too pedantic, so I'll
take your earlier recommendation and simply "ignore it". :)
 
D

Dave Weaver

Sherm Pendley said:
for ($counter=0;$counter <=8;$counter++){
if (defined $puzzle->{$element}[$counter]){$element_counter++}
...................................................^..................^

There's no opening bracket on that if(). The other errors are likely to be the
result of failed error recovery after the first error.

Yes there is. But the horrible code layout obscured it.
 
S

Sherm Pendley

Dave Weaver said:
Sherm Pendley said:
for ($counter=0;$counter <=8;$counter++){
if (defined $puzzle->{$element}[$counter]){$element_counter++}
..................................................^..................^

There's no opening bracket on that if(). The other errors are likely to be the
result of failed error recovery after the first error.

Yes there is. But the horrible code layout obscured it.

Oops, you're right. Sorry for the noise.

sherm--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,279
Latest member
LaRoseDermaBottle

Latest Threads

Top