On Tue, 28 Nov 2000, Aldo Calpini wrote:

> [EMAIL PROTECTED] wrote:
> > Hi.. I have a problem with the listbox widget. There doesn't
> > appear to be a way to tell when the first item of a listbox
> > is selected because it's index is 0. But in perl 0 also means
> > undefined
> 
> completely wrong! :-)
> 0 does not mean undefined, there is a small but significant
> difference between the two. consult the perldata manpage,
> under the section: Scalar values.

What a revelation! Thanks for setting me straight on that. :-)

> >           and if nothing is selected SelectedItems returns
> > undefined ie: 0 so there's no way to tell whether the first
> > item is selected or not. :( Can anyone think of a hack to
> > get around this?

> the 'hack' is provided by the defined() function.
> you can check first if there is any selected item with this
> instructions:

Hmm.. this didn't work. I'm guessing that SelectedItems() doesn't actually
return undefined then. (I was wrong again. ;) I tried:

if ($listbox->SelectedItems() ne "") {
        ...


which was suggested in the perldata manpage you mentioned and that works.
Perhaps SelectedItems() is returning a null array that is defined but
empty or something like that. I'm not really sure, but whatever it returns
-is- defined and isn't a 0. :-)

Thanks for the help!

-MattA


>     if(defined( $listbox->SelectedItems() )) {
>         @items = $listbox->SelectedItems();
>     } else {
>         # nothing is selected
>     }
> 
> BTW, if nothing is selected the function does not return 0.
> try this code:
>     
>     @items = $listbox->SelectedItems();
>     print "SelectedItems: ", join(", ", @items), "\n";
> 
> if nothing is selected, it will print:
>     SelectedItems:
> if the first item is selected, it will print:
>     SelectedItems: 0
> 
>     
> cheers,
> Aldo
> 
> __END__
> $_=q,just perl,,s, , another ,,s,$, hacker,,print;
> 
> 
> 

Reply via email to