On Aug 27, Timothy said:
>I am building an array of Tk button and each button will invoke the
>same subroutine. However I need the index of the calling button to
>be sent to the subroutine. So far I have a for loop and a SWITCH that
>that works but it seems to me to be pretty poor coding:
>
> for ($intIndex=0; $intIndex<20; $intIndex++) {
> SWITCH: {
> if ($intIndex==0) {
>$objButton=$objFrame->Button(-background=>$BG,-borderwidth=>'1',-command=>sub
>{&FileBrowse($objWindow,"0")},height=>'1',-text=>'Browse')->pack(-side=>'top',-anchor=>'w');
> last SWITCH; }
> --------------------
> --- 1 to 18 here ---
> --------------------
> if ($intIndex==19) {
>$objButton=$objFrame->Button(-background=>$BG,-borderwidth=>'1',-command=>sub
>{&FileBrowse($objWindow,"19")},height=>'1',-text=>'Browse')->pack(-side=>'top',-anchor=>'w');
> last SWITCH; }
> }
> push @arrButton, $objButton;
>
>As you can see the only thing that is changing is the constant "1" to "19"
>in the subroutine parameters. I can't just use $intIndex as that is local
>to this subroutine. Anyone see a simple concise way round this? TIA:)
You CAN use $indIndex in the code reference, IF it's my() variable:
for my $idx (0 .. 19) {
$objFrame->Button(
-background => $BG,
-borderwidth => 1,
-command => sub { FileBrowse($objWindow, $idx)} ,
-height => 1,
-text => 'Browse'
)->pack(-side => 'top' , -anchor => 'w');
}
--
Jeff "japhy" Pinyan [EMAIL PROTECTED] http://www.pobox.com/~japhy/
RPI Acacia brother #734 http://www.perlmonks.org/ http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
<stu> what does y/// stand for? <tenderpuss> why, yansliterate of course.
[ I'm looking for programming work. If you like my work, let me know. ]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]