SPUG: TK perl

thomas lems tmlems2000 at yahoo.com
Tue Sep 7 11:43:55 PDT 2010


I’m new to Perl, and i used  TK Perl to create a table with five columns and 30 rows.  I was able to save the output to a text file, but I’m struggling on how to keep the data in the text file cause every time I run the script I t override the previous data.
> I believe the issue it’s in the Sub Save Data procedure.
> #!/usr/local/bin/perl -w
> 
> # Table application example
> 
> use strict;
> 
> use Tk;
> use Tk::Button;
> use Tk::Entry;
> use Tk::Frame;
> use Tk::Pane;
> 
> my $file_name = 'data.txt';
> my $data = &ReadData($file_name);
> my $rows = 5;
> my $cols = 10;
> my (@headings) = ('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
> my (@width) = (18,30,16,15,19,14,12,13,15,19,12,17);
> #================= Tk part:
> my $mw=MainWindow->new(-title=>"Input table data");
> $mw->geometry('=700x250+120+1'); # initial window position
> #===vptk widgets definition===< DO NOT WRITE UNDER THIS LINE >===
> my $w_Frame_001 = $mw -> Scrolled ( 'Frame', -relief=>'flat', -scrollbars=>'osoe' ) -> pack(-anchor=>'n', -side=>'top', -fill=>'both', -expand=>1);
> my $w_Frame_002 = $mw -> Frame ( -relief=>'flat' ) -> pack(-anchor=>'n', -side=>'top', -fill=>'y', -expand=>1,-pady=>5);
> my $w_Button_001 = $w_Frame_002 -> Button ( -overrelief=>'raised', -relief=>'raised', -text=>'Dismiss', -compound=>'none', -command=>sub {exit}, -state=>'normal' ) -> pack( -side=>'left', -padx=>10);
> my $w_Button_002 = $w_Frame_002 -> Button ( -overrelief=>'raised', -relief=>'raised', -text=>'Save', -compound=>'none', -command=>[\&SaveData,$file_name,$data,$rows,$cols], -state=>'normal' ) -> pack( -side=>'left', -padx=>10);
> &FillTable($w_Frame_001,$data,$rows,$cols,\@headings,\@width);
> 
> MainLoop;
> 
> #===vptk end===< DO NOT CODE ABOVE THIS LINE >===
> sub ReadData {
>   my ($file_name)=@_;
>   my %data;
> 
>   return \%data unless -e $file_name;
>   open(DATA,$file_name) or die "$0 ERROR: read $file_name - $!";
>   my $i = 0;
>   while(<DATA>)
>   {
>     chomp;
>     my (@elements) = /<([^<>]*)>\s+/g;
>     for(my $j=0;$j<scalar(@elements);$j++)
>     {
>       $data{"$i,$j"} = $elements[$j];
>     }
>     $i++;
>   }
>   close DATA;
>   return \%data;
> }
> 
> sub SaveData {
>   my ($file_name,$data,$rows,$cols) = @_;
> 
>   open(DATA,">$file_name") or die "$0 ERROR: write $file_name - $!"; 
>   for (my $i=0;$i<$rows;$i++)
>   {
>     for (my $j=0;$j<$cols;$j++)
>     {
>       printf DATA ("<%s> ",$data->{"$i,$j"});
>     }
>     print DATA "\n";
>   }
>   close DATA;
>   exit;
> }
> 
> sub FillTable {
>   my ($container,$data,$rows,$cols,$headings,$width) = @_;
> 
>   for (my $j=0;$j<scalar(@$headings) && $j<$cols; $j++)
>   {
>     $container -> Label(-text=>$headings->[$j]) ->
>         grid(-row=>0, -ipady=>2, -ipadx=>2, -sticky=>'ew', -column=>$j);
>   }
>   for (my $i=0;$i<$rows;$i++)
>   {
>     for (my $j=0;$j<$cols;$j++)
>     {
>       my $Entry = $container -> 
>         Entry ( 
>           -justify=>'left', 
>           -relief=>'sunken', 
>           -validate=>'none', 
>           -state=>'normal', 
>           -width=>$width->[$j],
>           -textvariable=>\$data->{"$i,$j"} ) -> 
>         grid(-row=>$i+1, -ipady=>2, -ipadx=>2, -sticky=>'ew', -column=>$j);
>       $data->{"$i,$j"} = '' unless $data->{"$i,$j"};
>     }
>   }
> }
> Please help!
> Thanks
> 



      


More information about the spug-list mailing list