Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

updating a variable call in a hash table

by coopermc (Initiate)
on Feb 09, 2009 at 03:06 UTC ( #742324=perlquestion: print w/ replies, xml ) Need Help??
coopermc has asked for the wisdom of the Perl Monks concerning the following question:

Okay, so I've got this in a hash table:
"text" => <<"TEXTEND", We play the same game again, but with a different proportion of colore +d balls in Box K (see below). Everything else is the same. <p> You can choose between box U or box K, both containing 100 balls of five different colors. One ball will be drawn from the box you have + chosen. You win &#36;1,000 if a $color_opt ball is drawn. <p> Please select the box of your choice: U or K. If you think both boxes are equally attractive, you can select Indiffe +rent. TEXTEND
What I need is the ability to change the value of $color_opt later on in the script, and have the value automatically update in the hash definition. So let's say I set $colot_opt = "blue" at some point and print out the "text" key value, then it should display "blue", and not whatever $color_opt was initially defined to be. How do I do this? Thanks - Stephen

Comment on updating a variable call in a hash table
Download Code
Re: updating a variable call in a hash table
by Anonymous Monk on Feb 09, 2009 at 03:08 UTC
      Is there no simple way to do this w/o invoking a module?
        I guess here's an even simpler example:
        $color = "green"; $text = "The color is $color" $color = "blue"; print $text;
        I want the output to be "The color is blue". How do I make that happen?

        You could include some markup text in the string. When you come to render the HTML replace the markup text with the variable value. In its simplest version that could just entail using a regex.

        But of course, what you are building is your own template system. When you need to go beyond a couple of trivial variable substitutions you'll realize you should have used a module in the first place. Being really lazy often entails a little more work up front.


        Perl's payment curve coincides with its learning curve.
        Is there no simple way to do this w/o invoking a module?

        Sure there is!

        $sentence = q("The next color is $color."); $color = q(blue); print eval $sentence;
        You can learn how to be lazy later :-)

        Note: There can be security implications if $color is supplied by user input.

Re: updating a variable call in a hash table
by shmem (Canon) on Feb 09, 2009 at 13:25 UTC

    You could use an anonymous subroutine as the hash value for 'text':

    %hash = ( "text" => sub { my $color_opt = shift; return <<"TEXTEND", We play the same game again, but with a different proportion of colore +d balls in + Box K (see below). Everything else is the same. <p> You can choose between box U or box K, both containing 100 balls of five different colors. One ball will be drawn from the box you have + chosen. You win &#36;1,000 if a $color_opt ball is drawn. <p> Please select the box of your choice: U or K. If you think both boxes are equally attractive, you can select Indiffe +rent. TEXTEND }, ); print $hash{text}->("blue"); print $hash{text}->("chartreuse");

    That way the interpolation is done when you evaluate the hash value (i.e. call the subroutine).

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://742324]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (17)
As of 2014-09-19 16:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (143 votes), past polls