in reply to Need help with binding event in Tk

Maybe it's just too late for me to think clearly, but why are you using the ternary expression instead of an if/else? It's nice to assign a value, but you're not.

Also - and this is a guess based on the odd indenting - did you intend that the while loop and following two statements be executed when $key eq "year"?

Try it this way:

$results->bind('<ButtonRelease-1>'=> sub{ $list = $results->curselection; if ($key eq "year") { &show($bros->{$data}->[$list]) } else { $index = 0; while($bros->{$ydx[$list]}->[$index]->{$key} !~ /$data +/i){ $index++; } $yr = $ydx[$list]; &show($bros->{$ydx[$list]}->[$index]); });

Replies are listed 'Best First'.
Re^2: Need help with binding event in Tk
by Popcorn Dave (Abbot) on Jul 24, 2004 at 18:43 UTC
    I had assumed, and apparently a bit mistakenly, that the ternary could handle more than just variable assignments. I have changed it to an if-else and it's working now. I still can't understand why in the ternary wouldn't evaluate the true part of the expression but would the false.

    As far as the indentation, the while loop actually does activate when the key is not "year". The indentation was merely so that I didn't have a line that extended out 100+ characters and would wrap when I printed the code to look at it on paper.

    There is no emoticon for what I'm feeling now.
      The (or at least a) problem as I saw it is that as written:

      $key eq "year" ? &show($bros->{$data}->[$list]) : $index = 0; while($bros->{$ydx[$list]}->[$index]->{$key} !~ $data/i){ $index++; } $yr = $ydx[$list]; &show($bros->{$ydx[$list]}->[$index]);

      the indentation is hiding what's happening. When written:

      $key eq "year" ? &show($bros->{$data}->[$list]) : $index = 0; while($bros->{$ydx[$list]}->[$index]->{$key} !~ /$data/i){ $index++; } $yr = $ydx[$list]; &show($bros->{$ydx[$list]}->[$index]);

      it is much more obvious that the while loop and two following statements are executed no matter what value $key holds.