Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

<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?

Replies are listed 'Best First'.
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 hdb (Monsignor) 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 AnomalousMonk (Chancellor) 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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1046060]
Approved by hdb
[erix]: easiest setup is to run with PG* env variables set, and a custom .pgpass files (can be a $PGPASSFILE)
[Corion]: Hmm - "trust" sounds interesting, but I have still to find whether I can restrict that on a per-DB level ;)
[Corion]: erix: Sure, but SQLite doesn't even need that :)
[hippo]: Corion: Yes, you can. It's all in the pg_hba.conf
[erix]: yeah, but there is a lot SQLite cannot do :)
[hippo]: Database is the 2nd column.
shmem .oO( docker. docker. docker? docker. docker! docker )
[shmem]: at least, some Ook.
[erix]: another simple Pg thing is to running a database off a tsv (alas, readonly)
[erix]: I'm waiting for that zipfile-reading to show up in DBD::SQLite :)

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (14)
As of 2018-02-23 15:14 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (302 votes). Check out past polls.