antioch has asked for the wisdom of the Perl Monks concerning the following question:

I've been working on a new script and unfortunately come to a brick wall.

Basically I have a variable with a numerical value in it (ex. $food = "5";) that's passed to a subroutine.

Alright, so far so good.

But now I need the variable's contents (5) to be used in a new variable, which is assigned to something else.

Ex. $work_5 = "plumbing";

I've looked around but still can't find any solutions to my problem, although I'm sure it's simple.

Any help is greatly appreciated, Thanks!


Kevin
  • Comment on Using variable contents in new variable name

Replies are listed 'Best First'.
Re: Using variable contents in new variable name
by imp (Priest) on Dec 11, 2006 at 02:07 UTC
    Perl allows this through symbolic references... but their use results in messy code that is difficult to maintain, and violates strict (which should always be used).

    You should use a different type of data structure instead - arrays and hashes are very appropriate here.

    my @work_array; $work_array[5] = "plumbing"; my %work_hash; $work_hash{5} = "plumbing";

      If it turns out that you actually need symbolic references, and still want to use strict, you can put a "no strict refs" just before the symbolic reference work, which will temporarily disable strictness for symbolic references. Remember to re-enable them, though!

        Remember to re-enable them, though!

        Do it only in the scope of a small code block

        use strict; ... # strict in effect here ... { no strict q{refs}; # strict refs not in effect in the scope # of this code block so do something with # symbolic refs here ... } ... # strict refs back in effect again without having # to remember to re-enable it. ...

        But, as imp says, don't go there if you can possibly help it.

        Cheers,

        JohnGG

Re: Using variable contents in new variable name
by McDarren (Abbot) on Dec 11, 2006 at 02:16 UTC
    As imp points out, you really don't want to do this.
    Have a read of this excellent article by Dominus to understand why not.

    What you probably need instead is a hash. If you are not sure how to implement this, post a short snippet of the actual code.

    Cheers,
    Darren :)

      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!

        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
Re: Using variable contents in new variable name
by GrandFather (Saint) on Dec 11, 2006 at 02:23 UTC