in reply to Hash of Arrays using Perl::Tk

I think davidj is onto something there, but his version of your code won't work any better than your original, because there are too many other mistakes that he didn't try to fix.

You say "everything works fine except..." but the code you posted doesn't actually compile -- you're using a bunch of variables that have not been declared with "my", including "@array", whose "$i'th" element (which is undefined) is being assigned as text for a Label widget, and $w (or @w in davidj's version), which is being assigned two different widgets; you "tie %hash" and then later declare "my %hash" (and assign values).

How about you post the code that actually runs? Also, look at the four blocks of 5 lines each, where the only difference from one block to the next is an array index. That's where a "for" loop comes in real handy -- saves a lot of repetitive typing (or copy/paste/editing)

If your database will be using actual key fields (rather than just numbers like 1..4) then keep using %hash -- but if you're just going to be handling records by means of a sequenctial record number, use an array-of-arrays.

Reconfiguring the "normal" and "disabled" states of the "Next" and "Last" buttons can be done a lot more simply -- e.g.:

my $nxtState = my $prvState = 'normal'; if ( $i == $first_index ) { $prvState = 'disabled'; } elsif ( $i == $last_index ) { $nxtState = 'disabled'; } $next->configure(-state => $nxtState ); $back->configure(-state => $prvState );

Replies are listed 'Best First'.
Re^2: Hash of Arrays using Perl::Tk
by Raad (Acolyte) on Dec 01, 2004 at 14:22 UTC
    Your advice is well taken. I have substituted my part of code which deals with reconfiguring the normal and disabled states of the back and next buttons. And It's working beautifully. The one thing I am still struggling with is tying my hash so that it displays in the original order of the data structure. It seems that the ordering only affects the keys of the hash of hashes not the element keys which store anonymous arrays. Any way around that? This is just beyond my poor human grasp. Thanks for all your help.
Re^2: Hash of Arrays using Perl::Tk
by davidj (Priest) on Dec 01, 2004 at 08:33 UTC
    What other mistakes are you refering to? As I replied to another monk above, my code runs perfectly with the modifications I made. Run it and you will see. I agree that his design is not ideal, but with the corrections I made it does work as he wants it to. My intention was not to redesign what he has, but to fix the error that generated the incorrect output.
    davidj
      Well, yes, removing "use strict" is one way to "fix" the compilation errors I referred to, and I failed to notice that your version had taken this approach.

      Of course, this is usually not the approach that we would want to recommend for people who are having trouble getting their code right.