[sf-perl] Referencing a variable???

Peter Loo loopeter at yahoo.com
Tue Jul 28 16:47:50 PDT 2009


Thanks Steve.  Yes, putting the values in the parameter is a brilliant idea.  I will make the change.

--- On Tue, 7/28/09, Steve Fink <sphink at gmail.com> wrote:


From: Steve Fink <sphink at gmail.com>
Subject: Re: [sf-perl] Referencing a variable???
To: "San Francisco Perl Mongers User Group" <sanfrancisco-pm at pm.org>
Date: Tuesday, July 28, 2009, 5:25 PM


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



-----Inline Attachment Follows-----


_______________________________________________
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/8f3cc467/attachment.html>


More information about the SanFrancisco-pm mailing list