in reply to Creating variables for each array member

A container class seems more appropriate than an array, given that you are already working with classes, for example:
use ButtonJar; my $jar = ButtonJar -> new(); $jar -> add( $but1 );
package ButtonJar; sub new { my $self = shift; $self = {}; return bless $self; } sub add { # add a button to the "jar" my $self = shift; my $but = shift; $self -> { $but -> id() } = $but; sub remove { # remove a button my $self = shift; my $but = shift; delete $self -> { $but -> id() }; } sub getall ( my $self = shift; return keys %$self; # to return the $widget -> id # or could return values %$self or just %$self # depending on how you want to reference your buttons } 1;

-M

Free your mind

Replies are listed 'Best First'.
Re^2: Creating variables for each array member
by jdtoronto (Prior) on Jul 25, 2006 at 12:29 UTC
    Moron,

    But surely for your suggestion to have any value the Tk widgets would need an ID method, which they don't have. And having created the widgets and 'add'ed them into the container class how do you tell them apart again?

    jdtoronto

      Tk::Widget and scroll down to the section headed $widget->id

      And re 'how do you tell them apart...', I've now added some comments to show that this is just an example -- I would probably use the object reference and therefore store the object reference instead of the id, but it really makes no functional difference. I notice that someone else has suggested code to store them in a hash. In a way that is what I am suggesting, but take a look at the huge reduction in coding requirements when you use a class instead of just a hash.

      -M

      Free your mind

        It's not clear to me where the "huge reduction in coding requirements" stem from compared with using a hash or an array for storing the buttons. Would you care to create two small examples showing how your object leverages the code compared with either of the other two suggestions?


        DWIM is Perl's answer to Gödel