Is a hash the best method to do this?

Is a hash the best method to do this?

am 24.12.2007 19:23:43 von vorticitywolfe

Hello,

I have a gui which reads a lot of data and fills in a table of entry
widgets with the updated values for certain variables. I want to make
it more dynamic and be able to creates x by y number of entry widgets
on the fly. Right now, I have a program that works, but I have
thousands of lines of repeated code and it just keeps getting more
difficult to keep track of everything, particularly when I want to
change one thing. I wrote a previous post about defining variables
within a for loop, which sounded good to me until I read some of the
other posts on why this is bad. Hashes seem like they would work for
this, but as a novice, it seems a little over my head on how to
accomplish this.

From here on, I think the code can be cleaned up and I should only
have to define it once while still maintaining the flexibility to add
more rows/columns. Is that possible?

# N ROWS
################################################
@stn=(0...N-1);

my $left2a = $top->Frame()->pack(-side=>'left',-pady=>2,-padx=>7);
# N Columns
################################################
my $z0=$left2a->Label(-text=>'Time (Z)',)->pack();
my $z1=$left2a->Entry(-justify=>'center',-background=>'light green',-
width=>5,-borderwidth=>2,-relief=>'sunken')->pack();
my $z2=$left2a->Entry(-justify=>'center',-background=>'light green',-
width=>5,-borderwidth=>2,-relief=>'sunken')->pack();
my $z3=$left2a->Entry(-justify=>'center',-background=>'light green',-
width=>5,-borderwidth=>2,-relief=>'sunken')->pack();
my $z4=$left2a->Entry(-justify=>'center',-background=>'light green',-
width=>5,-borderwidth=>2,-relief=>'sunken')->pack();
my $z5=$left2a->Entry(-justify=>'center',-background=>'light green',-
width=>5,-borderwidth=>2,-relief=>'sunken')->pack();
my $z6=$left2a->Entry(-justify=>'center',-background=>'light green',-
width=>5,-borderwidth=>2,-relief=>'sunken')->pack();
my $z7=$left2a->Entry(-justify=>'center',-background=>'light green',-
width=>5,-borderwidth=>2,-relief=>'sunken')->pack();




# FIRST STATION
############################################################ ####
if ($string =~/$stn[0]/){
if($string =~ m/\d\d(\d\d\d\d)Z/){#if($string =~ m/(\d\d\/d\d)/){
if (defined($1)) {
$time=$1;
}

}else{
$time="M";
}
$z1->delete('0.0','end');
$z1->insert('1.0',$time);
}


I hope this is clear enough, if not feel free to ask. Any suggestion
is helpful.
Thanks,
Jonathan

Re: Is a hash the best method to do this?

am 24.12.2007 19:25:00 von vorticitywolfe

On Dec 24, 6:23 pm, vorticitywo...@gmail.com wrote:
> Hello,
>
> I have a gui which reads a lot of data and fills in a table of entry
> widgets with the updated values for certain variables. I want to make
> it more dynamic and be able to creates x by y number of entry widgets
> on the fly. Right now, I have a program that works, but I have
> thousands of lines of repeated code and it just keeps getting more
> difficult to keep track of everything, particularly when I want to
> change one thing. I wrote a previous post about defining variables
> within a for loop, which sounded good to me until I read some of the
> other posts on why this is bad. Hashes seem like they would work for
> this, but as a novice, it seems a little over my head on how to
> accomplish this.
>
> From here on, I think the code can be cleaned up and I should only
> have to define it once while still maintaining the flexibility to add
> more rows/columns. Is that possible?
>
> # N ROWS
> ################################################
> @stn=(0...N-1);
>
> my $left2a = $top->Frame()->pack(-side=>'left',-pady=>2,-padx=>7);
> # N Columns
> ################################################
> my $z0=$left2a->Label(-text=>'Time (Z)',)->pack();
> my $z1=$left2a->Entry(-justify=>'center',-background=>'light green',-
> width=>5,-borderwidth=>2,-relief=>'sunken')->pack();
> my $z2=$left2a->Entry(-justify=>'center',-background=>'light green',-
> width=>5,-borderwidth=>2,-relief=>'sunken')->pack();
> my $z3=$left2a->Entry(-justify=>'center',-background=>'light green',-
> width=>5,-borderwidth=>2,-relief=>'sunken')->pack();
> my $z4=$left2a->Entry(-justify=>'center',-background=>'light green',-
> width=>5,-borderwidth=>2,-relief=>'sunken')->pack();
> my $z5=$left2a->Entry(-justify=>'center',-background=>'light green',-
> width=>5,-borderwidth=>2,-relief=>'sunken')->pack();
> my $z6=$left2a->Entry(-justify=>'center',-background=>'light green',-
> width=>5,-borderwidth=>2,-relief=>'sunken')->pack();
> my $z7=$left2a->Entry(-justify=>'center',-background=>'light green',-
> width=>5,-borderwidth=>2,-relief=>'sunken')->pack();
>
> # FIRST STATION
> ############################################################ ####
> if ($string =~/$stn[0]/){
> if($string =~ m/\d\d(\d\d\d\d)Z/){#if($string =~ m/(\d\d\/d\d)/){
> if (defined($1)) {
> $time=$1;
> }
>
> }else{
> $time="M";
> }
> $z1->delete('0.0','end');
> $z1->insert('1.0',$time);
> }
>
> I hope this is clear enough, if not feel free to ask. Any suggestion
> is helpful.
> Thanks,
> Jonathan

sorry posted in wrong group this should go under Tk

Re: Is a hash the best method to do this?

am 24.12.2007 20:30:44 von jurgenex

vorticitywolfe@gmail.com wrote:
>I have a gui which reads a lot of data and fills in a table of entry
>widgets with the updated values for certain variables. I want to make
>it more dynamic and be able to creates x by y number of entry widgets
>on the fly. Right now, I have a program that works, but I have
>thousands of lines of repeated code and it just keeps getting more
>difficult to keep track of everything, particularly when I want to
>change one thing.

Repetitive code is bad. Well, you discovered that already, didn't you :-)
The solution is usually to use functions. Identify the code pieces that are
very similar, write a function for the code piece, and where there are
differences use arguments/variables.

>I wrote a previous post about defining variables
>within a for loop, which sounded good to me until I read some of the
>other posts on why this is bad. Hashes seem like they would work for
>this, but as a novice, it seems a little over my head on how to
>accomplish this.

Well, hashes are the generic solution whenever someone tries to use symbolic
references. However sometimes there are even easier approaches, see below.

>From here on, I think the code can be cleaned up and I should only
>have to define it once while still maintaining the flexibility to add
>more rows/columns. Is that possible?
>
># N ROWS
>################################################
>@stn=(0...N-1);
>
>my $left2a = $top->Frame()->pack(-side=>'left',-pady=>2,-padx=>7);
># N Columns
>################################################
>my $z0=$left2a->Label(-text=>'Time (Z)',)->pack();
>my $z1=$left2a->Entry(-justify=>'center',-background=>'light green',-
>width=>5,-borderwidth=>2,-relief=>'sunken')->pack();
>my $z2=$left2a->Entry(-justify=>'center',-background=>'light green',-
>width=>5,-borderwidth=>2,-relief=>'sunken')->pack();
>my $z3=$left2a->Entry(-justify=>'center',-background=>'light green',-
>width=>5,-borderwidth=>2,-relief=>'sunken')->pack();
>my $z4=$left2a->Entry(-justify=>'center',-background=>'light green',-
>width=>5,-borderwidth=>2,-relief=>'sunken')->pack();
>my $z5=$left2a->Entry(-justify=>'center',-background=>'light green',-
>width=>5,-borderwidth=>2,-relief=>'sunken')->pack();
>my $z6=$left2a->Entry(-justify=>'center',-background=>'light green',-
>width=>5,-borderwidth=>2,-relief=>'sunken')->pack();
>my $z7=$left2a->Entry(-justify=>'center',-background=>'light green',-
>width=>5,-borderwidth=>2,-relief=>'sunken')->pack();

Whenever you are enumerating variables like this you should be thinking
array. No need for a hash here, the whole chunk can be collapsed into 2
assignments:

$z[0]=$left2a->Label(-text=>'Time (Z)',)->pack();
for (1..7) {
$z[$_] = $left2a->Entry(-justify=>'center',-background=>'light
green',-width=>5,-borderwidth=>2,-relief=>'sunken')->pack();
}

jue