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

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!!
  • Comment on Re^4: Using variable contents in new variable name

Replies are listed 'Best First'.
Re^5: Using variable contents in new variable name
by chromatic (Archbishop) on Dec 11, 2006 at 03:45 UTC
    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
Re^5: Using variable contents in new variable name
by GrandFather (Saint) on Dec 11, 2006 at 03:59 UTC

    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