array value in for loop lost when mainloop happens 
Author Message
 array value in for loop lost when mainloop happens

I am writting a quick little cheater app in tk for slowaris 8.  All is
great except I am running into a bunch of commands I would like to add.
 I am trying to utilize an array to hold a list of command strings and a
for loop to cycle through and build my buttons.  If a use a static
variable the command runs fine, but if I use an array it appears to
loose the value in the element when the program goes to MainLoop.

Any thoughts?

Code to follow:

#!/usr/bin/perl -w -I /usr/local/lib/perl5/site_perl/5.6.0/sun4-solaris

use Tk;
use strict;

my (
    $i,


   );

my $font = '-adobe-courier-bold-r-normal--18-180-75-75-m-110-iso8859-1';

$runcommands[0] = "/usr/openwin/bin/xterm -sb -sl 4096 -bg black -fg
white -fn $font &";
$titles[0] = "X-term";
$runcommands[1] = sprintf("%s",'cconsole rockymtn &');
$titles[1] = "CC-MTN";
$runcommands[2] = sprintf("%s",'cconsole rockytad &');
$titles[2] = "CC-TAD";
$runcommands[3] = sprintf("%s",'crlogin rockytad &');
$titles[3] = "CR-TAD";

my $mw = MainWindow->new;
$mw->title("Robs Command Center");

for ($i=0; $i<=$#runcommands; $i++) {
  $mw->Button(
        -text => $titles[$i],
        -activebackground => 'blue',
        -activeforeground => 'white',
        -background => 'cyan',
        -foreground => 'black',
        -relief => 'groove',
        -command => sub { system($runcommands[$i]); }
        )->pack(
        -side => 'left',
        -expand => 1,
        -fill => 'x');

Quote:
}

$mw->Button(
        -text => "Exit",
        -activebackground => "yellow",
        -activeforeground => "red",
        -background => "red",
        -foreground => "white",
        -relief => 'groove',
        -command => sub { exit }
        )->pack(
        -side => 'left',
        -expand => 1,
        -fill => 'x');
MainLoop;

Sent via Deja.com
http://www.*-*-*.com/



Tue, 17 Jun 2003 02:11:19 GMT  
 array value in for loop lost when mainloop happens

Quote:
> I am writting a quick little cheater app in tk for slowaris 8.  All is
> great except I am running into a bunch of commands I would like to add.
>  I am trying to utilize an array to hold a list of command strings and a
> for loop to cycle through and build my buttons.  If a use a static
> variable the command runs fine, but if I use an array it appears to
> loose the value in the element when the program goes to MainLoop.

In your code:

        -command => sub { system($runcommands[$I]); }

$I is being evaluated when the sub is invoked.  You want $I evaluated
when you are building the Button so I recommend using the following
instead:

        -command => [sub { system($runcommands[$_[0]]); }, $I]

Quote:
> Any thoughts?

> Code to follow:

> #!/usr/bin/perl -w -I /usr/local/lib/perl5/site_perl/5.6.0/sun4-solaris

> use Tk;
> use strict;

> my (
>     $i,


>    );

> my $font = '-adobe-courier-bold-r-normal--18-180-75-75-m-110-iso8859-1';

> $runcommands[0] = "/usr/openwin/bin/xterm -sb -sl 4096 -bg black -fg
> white -fn $font &";
> $titles[0] = "X-term";
> $runcommands[1] = sprintf("%s",'cconsole rockymtn &');
> $titles[1] = "CC-MTN";
> $runcommands[2] = sprintf("%s",'cconsole rockytad &');
> $titles[2] = "CC-TAD";
> $runcommands[3] = sprintf("%s",'crlogin rockytad &');
> $titles[3] = "CR-TAD";

> my $mw = MainWindow->new;
> $mw->title("Robs Command Center");

> for ($i=0; $i<=$#runcommands; $i++) {
>   $mw->Button(
>         -text => $titles[$i],
>         -activebackground => 'blue',
>         -activeforeground => 'white',
>         -background => 'cyan',
>         -foreground => 'black',
>         -relief => 'groove',
>         -command => sub { system($runcommands[$i]); }
>         )->pack(
>         -side => 'left',
>         -expand => 1,
>         -fill => 'x');
> }

> $mw->Button(
>         -text => "Exit",
>         -activebackground => "yellow",
>         -activeforeground => "red",
>         -background => "red",
>         -foreground => "white",
>         -relief => 'groove',
>         -command => sub { exit }
>         )->pack(
>         -side => 'left',
>         -expand => 1,
>         -fill => 'x');
> MainLoop;

> Sent via Deja.com
> http://www.deja.com/

--
Marc Dashevsky  (remove "_" from address to reply by e-mail)


Tue, 17 Jun 2003 03:24:32 GMT  
 array value in for loop lost when mainloop happens


Quote:

> > I am writting a quick little cheater app in tk for slowaris 8.  All
is
> > great except I am running into a bunch of commands I would like to
add.
> >  I am trying to utilize an array to hold a list of command strings
and a
> > for loop to cycle through and build my buttons.  If a use a static
> > variable the command runs fine, but if I use an array it appears to
> > loose the value in the element when the program goes to MainLoop.

> In your code:

>    -command => sub { system($runcommands[$I]); }

> $I is being evaluated when the sub is invoked.  You want $I evaluated
> when you are building the Button so I recommend using the following
> instead:

>    -command => [sub { system($runcommands[$_[0]]); }, $I]

> --
> Marc Dashevsky  (remove "_" from address to reply by e-mail)

I actually accomplished the same thing using a modified example from the
perldocs.  I used the foreach instead of the for, this forces me to not
use the $i reference.  Additionally, I utilize an outside sub for the
running of the command.  This is working great.  Now I just need to get
rid of the window border.  This is being displayed in Slowaris 5.8, CDE.

New code follows:

#!/usr/bin/perl -w -I /usr/local/lib/perl5/site_perl/5.6.0/sun4-solaris

use Tk;
use strict;

my $i;
my (
    $count,


   );
$count=0;
my $font = '-adobe-courier-bold-r-normal--18-180-75-75-m-110-iso8859-1';

$runcommands[0] = "/usr/openwin/bin/xterm -sb -sl 4096 -bg black -fg
white -fn $font &";
$titles[0] = "X-term";
$runcommands[1] = sprintf("%s",'cconsole rockymtn &');
$titles[1] = "CC-MTN";
$runcommands[2] = sprintf("%s",'cconsole rockytad &');
$titles[2] = "CC-TAD";
$runcommands[3] = sprintf("%s",'crlogin rockytad &');
$titles[3] = "CR-TAD";

my $mw = MainWindow->new;
$mw->title("Robs Command Center");


  $mw->Button(
        -text => $titles[$count],
        -activebackground => 'blue',
        -activeforeground => 'white',
        -background => 'cyan',
        -foreground => 'black',
        -relief => 'raised',
        -command => [ \&runcmd, $_ ]
        )->pack(
        -side => 'left',
        -expand => 1,
        -fill => 'x');
  $count++;

Quote:
}

$mw->Button(
        -text => "Exit",
        -activebackground => "yellow",
        -activeforeground => "red",
        -background => "red",
        -foreground => "white",
        -relief => 'groove',
        -command => sub { exit }
        )->pack(
        -side => 'left',
        -expand => 1,
        -fill => 'x');
MainLoop;

sub runcmd {
  system ("$_[0]");

Quote:
}

Sent via Deja.com
http://www.deja.com/


Tue, 17 Jun 2003 04:17:36 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. global my() variable used in foreach() loop loses its value

2. First array value lost with LWP::UserAgent

3. how do I modify the value in an array within a foreach loop

4. Value in array with loop

5. Accessing array values after loop has concluded (repost)

6. Problem accessing array values with button after loop has concluded

7. Using Tk and sockets --MainLoop looping problem.

8. splitting array, each element holding multiple values into array with single values

9. hash of values and array of hashes of values and array of hashes

10. Mainloop and Tk::Mainloop

11. Loop with Array or Loop and Read File?

12. Losing value in require statement

 

 
Powered by phpBB® Forum Software