in reply to Sorting data structure

Thanks all for the replies. Using the solution given, I'm trying to iterate over the values like so:
for my $user ((sort { $users{$b}->{count} <=> $users{$a}->{count} } ke +ys %users) { blah blah... }
But I get compile time errors
Global symbol "$user" requires explicit package name at
Is the problem obvious? Right now I'm being forced to sort to a list, then iterated over the items in the list:
my @sorted_users = sort { $users{$b}->{count} <=> $users{$a}->{count} } keys %users; for (@sorted_users) { blah... blahh }

Replies are listed 'Best First'.
Re^2: Sorting data structure
by davido (Cardinal) on Oct 14, 2005 at 15:33 UTC

    Does the symbol $user appear somewhere outside of the loop? Its scope is constrained to inside the loop. If you try to access $user outside the loop you'll get a fatal error under strictures.

    Also check to ensure that the line preceeding your for loop isn't missing a semicolon at the end of the line. ;)


    Dave

      Here is the entire loop:
      for my $location (sort {$a cmp $b} keys %user_locations) { $worksheet->write($row++, $col, "$location ($user_locations{$loca +tion})", $format); my @sorted_users = sort { $users{$b}->{count} <=> $users{$a}->{count} } keys %users; for my $user (for my $user ((sort { $users{$b}->{count} <=> $users +{$a}->{count} } keys %users) { next unless $users{$user}->{suffix} eq $user_locations{$locati +on}; for (my $tmp=1; $tmp <= keys %sheet_layout; $tmp++) { $worksheet->write($row, $col++, $users{$user}->{$sheet_lay +out{$tmp}->[0]}, $format_r); } $col = 0; $row++; } $row++; }
      I can't find the problem right now, maybe I should stare at it a while longer...
        Ok, pointing out the obvious thing that jumps out at me, is this nasty line doing the right thing:
        for my $user (for my $user ((sort { $users{$b}->{count} <=> $users{$a} +->{count} } keys %users) {
        A nested for using the same variable $user? Yes, I realize they don't kill one another due to the scoping, provided you don't really care about the outer $user variable within the inner for, but that's really nasty.

        Looking just slightly more carefully, it looks like the outer for block is never started... Is it simply a cut and paste problem, or does the real code really look that way?

        -Scott