Question

The ordinary and simple "newbie perl code does not work and writer has no idea why" question:

sub print_hash {
    our %hash;
    my @list = sort {$hash{$a} <=> $hash{$b} keys %hash;            
    foreach my $name (@list) {
        printf "$name $hash{$name}";
    }   
}

error:

syntax error at ./freq line 27, near "my "
Global symbol "@list" requires explicit package name at ./freq line 27.
BEGIN not safe after errors--compilation aborted at ./freq line 27.

I assume I somehow messed my scopes up. Adding our %hash to the sort eval did not help, at all (threw more errors), and outsourcing it to a sub did the same. This code is almost directly stolen from an hash sorting tutorial. I would really appreciate an explanation of how I went wrong!

Was it helpful?

Solution

Two mistakes:

our %hash; # a semicolon missing
my @list = sort {$hash{$a} <=> $hash{$b}} keys %hash; 
                                        ^ a curly bracket!

OTHER TIPS

You're missing a semicolon after our %hash.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top