in reply to Hash of Arrays using Perl::Tk

Your problem is this: in your sub process_rows you are assigning the values to $w and $w refers to the label holding the state value. Actually all the values in each array are getting (re)assigned to it. The solution is to have a unique name for each of the labels. You can do it using an array. See my modification below (and notice that each label is identified by an array element of @w:
use Tk; use Tie::IxHash; tie %hash, "Tie::IxHash"; my $i=1; my @w; #array to hold labels my %hash =( 1=>["Sappy", "Jewell", "2515 E Princeton", "Visalia", "CA" ], 2=>["Roomy", "Hilton", "12803 Westledge Ln", "Fifa", "Transvania"], 3=>[ "Lala", "Deena", "423 Northland Street", "St. Louis", "Misery" ], 4=>[ "LOLO", "Homy", "444 Wold City", "Foxy", "Flowerida" ], ); my @fields = qw/First_Name Last_Name Address City State /; my $mw=MainWindow->new (-title=>"Data Test"); my $rec_no=keys %hash; my $label = $mw->Label(-text=>"$array[$i]"); $label->pack; my $t=$mw->Scrolled("Text",-width=>50,-wrap=>'none')->pack(-expand=>1, +-fill=>'both'); $w[0]=$t->Label(-text=>"$fields[0]",-relief=>'groove',-width=>20); $t->windowCreate('end',-window=>$w[0]); $w[0]=$t->Entry(-width=>20,-textvariable=>\$hash{$i}[0]); $t->windowCreate('end',-window=>$w[0]); $t->insert('end',"\n"); $w[1]=$t->Label(-text=>"$fields[1]",-relief=>'groove',-width=>20); $t->windowCreate('end',-window=>$w[1]); $w[1]=$t->Entry(-width=>20,-textvariable=>\$hash{$i}[1]); $t->windowCreate('end',-window=>$w[1]); $t->insert('end',"\n"); $w[2]=$t->Label(-text=>"$fields[2]",-relief=>'groove',-width=>20); $t->windowCreate('end',-window=>$w[2]); $w[2]=$t->Entry(-width=>20,-textvariable=>\$hash{$i}[2]); $t->windowCreate('end',-window=>$w[2]); $t->insert('end',"\n"); $w[3]=$t->Label(-text=>"$fields[3]",-relief=>'groove',-width=>20); $t->windowCreate('end',-window=>$w[3]); $w[3]=$t->Entry(-width=>20,-textvariable=>\$hash{$i}[3]); $t->windowCreate('end',-window=>$w[3]); $t->insert('end',"\n"); $w[4]=$t->Label(-text=>"$fields[4]",-relief=>'groove',-width=>20); $t->windowCreate('end',-window=>$w[4]); $w[4]=$t->Entry(-width=>20,-textvariable=>\$hash{$i}[4]); $t->windowCreate('end',-window=>$w[4]); $t->insert('end',"\n"); $next =$mw->Button ( -relief=>'raised', -text=>"Next >>", -state=>'nor +mal',-command=>\&next ); $back =$mw->Button(-relief=>'raised', -text=>"<< Back", -state=>'norma +l',-command=>\&back ); &process_rows(); print Dumper(%hash); MainLoop; sub process_rows{ if ($i ==1){ $back->configure(-text=>"Back", -state=>'disabled',-command=>\ +&back); $next ->configure(-text=>"Next >>", -state=>'normal',-command= +>\&next); } elsif ($i == $rec_no){ $next->configure(-text=>"Next", -state=>'disabled',-command=>\ +&next); $back->configure(-text=>"Back", -state=>'normal',-command=>\&b +ack); } else { $next->configure(-text=>"Next", -state=>'normal',-command=>\&n +ext); $back->configure(-text=>"Back", -state=>'normal',-command=>\&b +ack); } for my $k (0..4){ $label->configure(-text=>"$i"); $label->pack; print $hash{$i}[$k] . "\n"; $w[$k]->configure(-textvariable=>\$hash{$i}[$k]); $t->configure(); $back->pack(-anchor=>'center', -side=>'left'); $next->pack(-anchor=>'center', -side=>'right'); } } sub next{ $i++ unless $i == $rec_no; &process_rows(); } sub back{ --$i unless $i == 1; &process_rows(); }
Hope this helps,
davidj

Replies are listed 'Best First'.
Re^2: Hash of Arrays using Perl::Tk
by pg (Canon) on Dec 01, 2004 at 05:08 UTC

    His code does not work, a well as yours. Other than that my is needed for quite a few places, if you search his code, $array appeared only once, and it is on the rght side of some assignment.

      You are incorrect. My code works. First, if you notice I removed use strict;, so my is not needed. Second, with the modifications I made, it runs perfectly on my system.
      davidj
Re^2: Hash of Arrays using Perl::Tk
by Raad (Acolyte) on Dec 01, 2004 at 14:02 UTC
    Davidj, Thank you for fixing the code. It does work in fact. I only had to add "use Data::Dumper" for it to work without errors, which I am assuming was added for debugging purposes. Thank you for alerting me to the array of labels I should have used. The original code compiled just fine on my Windows machine, even though I am using use strict. I believe it has to do with my IDE (PerlBuilder) which does not throw errors sometimes. I really appreciate your help.