in reply to Re: Using variable contents in new variable name
in thread Using variable contents in new variable name

Yeah, I had a feeling what I needed was a hash but like you said, I have no idea how to implement it in this situation.

Not sure how much this snippet will help (I'm also working in Tk) but:

## This is the button that initiates the sub ADMISSION $COLUMN_1->Button(-image=>$admiss_1, -width=>179, -border=>0, -background=>'#c2d297', -relief=>'flat', -command=>sub{ ADMISSION('1') } ) ->pack(-side=>'top'); ## This is part of the sub ADMISSION that contains a text field ($name +field) that the variable "$admiss_?_name" needs to be assigned to.(Wi +th the question mark being replaced by the "1" being passed by the bu +tton) sub ADMISSION { $namefield->Text(-width=>30,-height=>1)->pack(); }
So ideally, the text field in the sub would be assigned like this

$admiss_1_name = $namefield->Text(-width=>30,-height=>1)->pack();

Thanks for your help guys!

Replies are listed 'Best First'.
Re^3: Using variable contents in new variable name
by GrandFather (Saint) on Dec 11, 2006 at 02:59 UTC

    Your sample line becomes:

    $admiss_ref->{name} = $namefield->Text(-width=>30,-height=>1)->pack();

    Or if, as seems to be the case, there are a number of these things in an array you could:

    $COLUMN_1->Button(, ... -command=>[\&ADMISSION, \%admiss, 1]) ->pack(-side=>'top'); ... sub ADMISSION { my ($admiss_ref, $index) = @_; $admiss_ref->[$index]{name} = ... }

    DWIM is Perl's answer to Gödel
      Thanks a lot! Helps clear some things up..

      I still don't completely understand what's going on in the sub though..

      I see that the hash %admiss and the variable "1" are being passed but I don't get how "$admiss_ref->$index{name}" ends up being "$admiss_1_name"

      Sorry if this is pushing it, but I just want to understand how this works in hope that I'll know how to incorporate more parameters to pass and be able to assign "$admiss_1_name", "$admiss_1_date", "$admiss_1_service" and so on to their appropriate text fields. (This process is being repeated for 27 others ex. $admiss_4_name, $admiss_4_date, $admiss_4_service..)

      Thanks again!!
        I don't get how $admiss_ref->[$index{name}] ends up being $admiss_1_name....

        It doesn't. It's a completely different data structure. $admissref is a reference to an array. $index{name} contains an index into that array. It's equivalent to $admiss[1] or something similar.

        Perhaps you should read References Quick Reference, if that's unclear.

        A more fully worked example may help:

        use strict; use Tk; my @booths; my $mw = MainWindow -> new; for (0..5) { $booths[$_]{button} = $mw->Button ( -text => "Booth $_", -command => [\&Vote, \@booths, $_] )->pack (); } $mw->Button (-text => 'Done', -command => sub {$mw->destroy ()})->pack + (); MainLoop; sub Vote { my ($boothsRef, $boothNum) = @_; my $count = ++$boothsRef->[$boothNum]{count}; $boothsRef->[$boothNum]{button}->configure (-text => "Booth $booth +Num ($count)"); }

        Note that the thing passed in is actually an array, but it could be a HoH or any other suitable structure.


        DWIM is Perl's answer to Gödel