[sf-perl] Referencing a variable???

Steve Fink sphink at gmail.com
Tue Jul 28 16:25:18 PDT 2009


If you restructure your message, you'll answer yourself:

> $$parmName has no value.  What am I doing wrong?

> I have 10 program variables namely $parm[1-10]

Yep, that's what you're doing wrong.

Your code should work. The problem must be earlier. But it's not the best
way to go about this anyway. You would be much better off putting those
parameters into an array @params and then

  for my $param (@params) {
    my ($hardValue, $replaceValue) = split(/~!~/, $param);
    $sqlString =~ s/~$hardValue~/$replaceValue/g;
  }

or if you really need to insert them positionally,

  for my $param (@params) {
    if (defined $param) {
      my ($hardValue, $replaceValue) = split(/~!~/, $param);
      $sqlString =~ s/~$hardValue~/$replaceValue/g;
    }
  }

The huge, huge benefit, beyond slightly prettier code, is that you can then
put a "use strict;" at the top and find mistakes like the spelling mistake
that is probably causing your current problem.


2009/7/28 Peter Loo <loopeter at yahoo.com>

> Hi All,
>
> I have done this once before and can't seem to remember how I did it.
>
> I have 10 program variables namely $parm[1-10].  Then I try to check if
> they are set using a loop like so:
>
>   for ( my $x = 1; $x <= $maxParm; $x++ ) {
>     my $parmName = "parm" . $x;
>     if ( $$parmName ) {
>       my ($hardValue, $replaceValue) = split(/~!~/, $$parmName);
>       $sqlString =~ s/~$hardValue~/$replaceValue/g;
>       }
>     }
>
> $$parmName has no value.  What am I doing wrong?
>
> Thank you all in advance.
>
> Peter
>
>
> _______________________________________________
> SanFrancisco-pm mailing list
> SanFrancisco-pm at pm.org
> http://mail.pm.org/mailman/listinfo/sanfrancisco-pm
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/sanfrancisco-pm/attachments/20090728/db88881f/attachment.html>


More information about the SanFrancisco-pm mailing list