Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

<p>string variable</p>

by coss (Initiate)
on Jul 24, 2013 at 10:35 UTC ( #1046060=perlquestion: print w/ replies, xml ) Need Help??
coss has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I have a question that I'd like to know why isn't possible do w +hat I pretend to do. It's reading a variable from an array to use in +an expression. Example: my @array = ('A1', 'A2', 'A3', $1, '$2'); my $message = 'If you choose a yellow car everyone will follow you!'; my $my_trigger = 'If you (.*) a (.*) car (.*) will follow you!'; if ($message =~ m/$my_trigger/g) { my $var1 = \$array[3]; my $option = \${$var1}; my $color = $2; my $whom = $3; print "option: " . $option; print "\ncolor: " . $color; print "\nwhom: " . $whom; } Why can I use inside the if $1, $2, $3 ... but can't use reading that +value from an array. The question might not make sense for lot of peo +ple, but I am curious why doesn't work... only return to me as a stri +ng and not a variable. It's possible to return as a variable?

Comment on <p>string variable</p>
Download Code
Re: <p>string variable</p>
by hdb (Prior) on Jul 24, 2013 at 10:45 UTC

    I am not sure about the wisdom of your undertaking but this is how you could be doing it (if I understand the question correctly...):

    use strict; use warnings; my @array = ('A1', 'A2', 'A3', \$1, '$2'); my $message = 'If you choose a yellow car everyone will follow you!'; my $my_trigger = 'If you (.*) a (.*) car (.*) will follow you!'; if ($message =~ m/$my_trigger/g) { my $option = ${$array[3]}; my $color = $2; my $whom = $3; print "option: " . $option; print "\ncolor: " . $color; print "\nwhom: " . $whom; }
Re: <p>string variable</p>
by mtmcc (Hermit) on Jul 24, 2013 at 10:47 UTC
    What outcome do you expect to get from that script?
Re: <p>string variable</p>
by daxim (Chaplain) on Jul 24, 2013 at 10:59 UTC
    You're asking about varvarnames. Read that article and the following ones to understand why we can't recommend that.

    Instead just use a hash!

    my @slots = qw(option color whom); my $message = 'If you choose a yellow car everyone will follow you!'; my $my_trigger = qr/If you (.*) a (.*) car (.*) will follow you!/; my %hash; if (@hash{@slots} = $message =~ /$my_trigger/) { for my $slot (@slots) { print "$slot: $hash{$slot}\n"; } }
Re: <p>string variable</p>
by AnomalousMonk (Abbot) on Jul 24, 2013 at 14:23 UTC

    Others have said, and I agree, that what you seem to want to do (which, by the way, is generally called a "templating system"; see, e.g., Text::Template) is, in the context in which you want to do it, a Bad IdeaSM. (But there's nothing wrong with the general notion of templating systems — given the right application!) An example of a quick-and-dirty implementation is given below.

    Others have suggested some variation on the use of hashes, and hash access is part of the "named capture group" facility of the regex engine of Perl versions 5.10+. See the discussion of the  (?<NAME>pattern) construct in the Extended Patterns section of perlre, and also the example below. See also the associated  %- and  %+ hashes in perlvar. Named capture groups can be quite convenient, but in regexes that do a lot of work, there may be a significant price to pay: YMMV.

    >perl -wMstrict -le "my @array = qw(A1 A2 A3 $1 $2); ;; my $message = 'If you choose a yellow car everyone will follow you!'; my $my_trigger = qr{If you (.*) a (?<COLOR>.*) car (?<WHOM>.*) will follow you!}; ;; if ($message =~ $my_trigger) { ;; my $option = eval qq{ qq{$array[3]} }; my $color = qq{color was: '$-{COLOR}[0]'}; my $whom = $+{WHOM}; ;; print qq{option of '$array[3]' was: '$option'}; print $color; print qq{whom: '$whom'}; } else { print 'no match'; } " option of '$1' was: 'choose' color was: 'yellow' whom: 'everyone'
      This explanation it's great and better then I thought. Thank you for your reply

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2014-12-27 04:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (176 votes), past polls