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

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

Replies are listed 'Best First'.
Re^4: Using variable contents in new variable name
by antioch (Sexton) on Dec 11, 2006 at 03:41 UTC
    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.

        Alright, thanks. I've got everything working now! =)

        The Quick Reference definitely helped out.

        -Kevin

      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