Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

canvas for loop problem

Status
Not open for further replies.

vquick

IS-IT--Management
Jul 13, 2008
14
0
0
US
I have 14 canvas's. they are names $canvas1 through $canvas14. I want to delete all of those canvas without putting delete for all 14 canvas. Here what i tried:

for (1 ..14){
my $canvas = "\$canvas$_";
$canvas->delete("all");
}

When i print $canvas is looks correct, but when i delete it tells me $canvas is unitialized value.

Is there a better way to do this?
 
It looks like you are using a module with an OO interface. $canvas would be the object not the name of the file to delete. Something like:

Code:
my $canvas = ModuleName->new();
for (1..14) {
   $canvas->delete(something to delete);
}


Which module are you using that has the delete method?


------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Im using Canvas here is a example

my $canvas1 = Canvas(-width => 500, -height => 500)->pack;

Then i have text on the canvas and i want to delete it.

$canvas1->delete("all");

This works but i have 14 canvas's i like to delete from. Rather then typing delete 14 times i would rather do some kind of for loop.

 
Maybe:

Code:
for ($canvas1, $canvas2, ..... $canvas14) {
   $_->delete("all");
}

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
that works, but is there a way to not have to put all 14 canvas's in the for loop.

by the way thanks for the replies
 
I don't think you can because $canvas is either a reference or an object.


------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
A better way to go about your code is to use an array for your canvases instead...

Code:
$canvas[0] = $mw->Canvas(...);
$canvas[1] = $mw->Canvas(...);
...

for (0..13) {
   $canvas[$_]->delete("all");
}

But if you must do it the hard way, you can do eval or something.

Code:
for (1..14) {
   my $code = qq{\$canvas$_->delete("all");};
   #print "exec: $code\n";
   eval ($code);
}

should end up effectively executing

Code:
$canvas1->delete("all");
$canvas2->delete("all");
...
$canvas14->delete("all");

Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
Kirsle

I'd probably go with the array solution, too, much cleaner, but why bother counting them?
Perl:
for my $canvas(@canvases) {
   $canvas->delete('all');
}

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top