Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variable variables?

Status
Not open for further replies.

rdcss

Programmer
Jun 24, 2004
17
US
I have the following in my script:

foreach $name ( param() ) {
$value=$dbh->quote(param($name));
print "$name = $value ";
}


I need each $name to also be listed as variable $$name... is there any way to tell it to do that? I tried adding a line
$$name=$name
but that did not work.

Right now, $name=name and $value='value' (value is correct, name needs to be $name rather than just name).

I hope I am stating clearly enough what I need.
 
hi,

i'm not too sure that i understand ...

applying $$name=$name will create a variable based on the value of $name

Code:
$name="john";
$$name=$name;

# will create variable called $john 

# or if you wish

@names=qw(john mike steven derek);

foreach (@names)
    {
    $$_=$_;
    }

# will create variables $john, $mike, $steven and $derek

I don't think I have actually helped you here???



 
Right, exactly! Except it didn't work :)

Here's what I'm doing - I'm passing a set of variables from one part of the script to the next. (I'm using one script, changing actions via CASE.) Rather than doing it the way I used to - listing each variable and assigning the param to it - I am trying to do it more simply with a foreach.

In other words, this:

foreach $name ( param() ) {
$value=$dbh->quote(param($name));
print "$name = $value ";
}

is replacing this:

$variable1=$dbh->quote(param('variable1'));
$variable2=$dbh->quote(param('variable2'));
$variable3=$dbh->quote(param('variable3'));

so I need both the value AND the name to be accurate. $variable1 is the name... but the above foreach is returning variable1 instead of $variable1

Does that explain it more clearly?

BTW - someone else said I could do ${$name}=$name but that doesn't seem to have worked either.
 
Code:
foreach $name ( param() ) {
    $value=$dbh->quote(param($name));
    print "$name = $value\n";
    eval("$name=$value");
    push (@variables, $name);
    push (@values, $value);
}

Is this what you're looking for?

This is fraught with its own dangers cause you're not necessarily aware of what variables are being passed unless you use the arrays

HTH
--Paul


It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
How do I get each variable out of @variables as a $variable? That code made
@variables
contain
v1v2v3v4v5 (etc)
but I need
$v1 $v2 (etc)

How do I access them that way?
 
Code:
$loop=0;
foreach(@variables) {
 print "$variables[$loop] is $values[$loop]\n";
 $loop++;
}
HTH
--Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
It still isn't doing what I need. Here is more info.

Code:
sub update_closing {
my ($name, $value, $params, %params, @variables, $variables, $loop, @values, $values);
	$params{$name} = $value; 

	foreach $name ( param() ) {
	$value=$dbh->quote(param($name));
	print "$name = $value ";
	push (@variables, $name);
	push (@values, $value);
	}
	$loop=0;
	foreach(@variables) {
	print "$variables[$loop]=$values[$loop]\n";
 	$loop++;
	}

#	$dateclosed=$dbh->quote(param('dateclosed'));
#	$houseadd=$dbh->quote(param('houseadd'));
#	$sellfn=$dbh->quote(param('sellfn'));
#	$sellln=$dbh->quote(param('sellln'));
#	$sellstr=$dbh->quote(param('sellstr'));
#	$sellcity=$dbh->quote(param('sellcity'));
#	$sellst=$dbh->quote(param('sellst'));
#	$sellzip=$dbh->quote(param('sellzip'));
#	$buyfn=$dbh->quote(param('buyfn'));
#	$buyln=$dbh->quote(param('buyln'));
#	$buystr=$dbh->quote(param('buystr'));
#	$buycity=$dbh->quote(param('buycity'));
#	$buyst=$dbh->quote(param('buyst'));
#	$buyzip=$dbh->quote(param('buyzip'));
#	$mls=$dbh->quote(param('mls'));
#	$debcomm=$dbh->quote(param('debcomm'));
#	$patcomm=$dbh->quote(param('patcomm'));
#	$lister=$dbh->quote(param('lister'));
#	$surveyor=$dbh->quote(param('surveyor'));
#	$closingatt=$dbh->quote(param('closingatt'));	
#	$pestco=$dbh->quote(param('pestco'));
#	$soldprice=$dbh->quote(param('soldprice'));

#print "$dateclosed, $houseadd, $sellfn, etc";

	$sth=$dbh->do("UPDATE closings 
	SET dateclosed=$dateclosed,houseadd=$houseadd,sellfn=$sellfn,
	sellln=$sellln,sellstr=$sellstr,sellcity=$sellcity,sellst=$sellst,
	sellzip=$sellzip,buyfn=$buyfn,buyln=$buyln,buystr=$buystr,
	buycity=$buycity,buyst=$buyst,buyzip=$buyzip,mls=$mls,debcomm=$debcomm,
	patcomm=$patcomm,lister=$lister,surveyor=$surveyor,closingatt=$closingatt,
	pestco=$pestco,soldprice=$soldprice	
	WHERE id=$id")
	or bail_out("Can't update closing");
	view_closings();
}

With all of the solutions I've been offered so far, I can get the variables' values fine, but the UPDATE to the database is not working because the values are not assigned to the correct $name, apparently. Either I need to know what name they ARE assigned to, or I need to know how to assign a name.

The section with # in front is how I used to do it, but that doesn't work here, either, because some of the variables might be null and the $dbh->quote function doesn't like null values.

 
I'm not sure what you're trying to do here
#define variables for use
my ($name, $value, $params, %params, @variables, $variables, $loop, @values, $values);
#$values is implied as an unsubscripted element of the array @values, so you can drop $values and $variables from your declaration, likewise $params, as it's implied in %params
$params{$name} = $value;
#what's this line to do, $value doesnt have a value at this stage.
foreach $name ( param() ) {
$value=$dbh->quote(param($name));
print "$name = $value ";#is this printing anything out?
push (@variables, $name);
push (@values, $value);
}

$loop=0;
foreach(@variables) {
print "$variables[$loop]=$values[$loop]\n";
$loop++;
}

--Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
I'm not sure what you're trying to do here
#define variables for use
my ($name, $value, $params, %params, @variables, $variables, $loop, @values, $values);
#$values is implied as an unsubscripted element of the array @values, so you can drop $values and $variables from your declaration, likewise $params, as it's implied in %params
$params{$name} = $value;
#what's this line to do, $value doesnt have a value at this stage.
foreach $name ( param() ) {
$value=$dbh->quote(param($name));
print "$name = $value ";#is this printing anything out?
push (@variables, $name);
push (@values, $value);
}

$loop=0;
foreach(@variables) {
print "$variables[$loop]=$values[$loop]\n";
$loop++;
}

--Paul
Paul, I'm not really sure how to do it so most of that first bit is bits and pieces from other people's suggestions.

I just included everything in the declaration so it wouldn't give me an Internal Server Error :)

The $params($name) = $value was an obviously mistaken attempt to achieve the $name=$value that I'm trying to get. Will remove that, thanks.

Yes, it is printing it out correctly, but when it gets to the update statement, the variables are all null, thus causing an error in the syntax, as some of the fields are "not null" fields (nor should they BE null, as the information IS being passed from the other part of the script, since it's printing out correctly).
 
which values aren't being passed
include this print statement before your update
Code:
print "dateclosed=$dateclosed\nhouseadd=$houseadd\nsellfn=$sellfn\nsellln=$sellln\nsellstr=$sellstr\nsellcity=$sellcity\nsellst=$sellst\nsellzip=$sellzip\nbuyfn=$buyfn\nbuyln=$buyln\nbuystr=$buystr\nbuycity=$buycity\nbuyst=$buyst\nbuyzip=$buyzip\nmls=$mls\ndebcomm=$debcomm\npatcomm=$patcomm\nlister=$lister\nsurveyor=$surveyor\nclosingatt=$closingatt\npestco=$pestco\nsoldprice=$soldprice\nid=$id";

HTH
--Paul


It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
Wouldn't it be a lot simpler just to use a hash?
Code:
my %value;
foreach $name ( param() ) {
    $value{$name}=$dbh->quote(param($name));
}

    $sth=$dbh->do("UPDATE closings 
    SET dateclosed=$value{'dateclosed'},
        houseadd=$value{'$houseadd'},
# ... etc ...
WHERE id=$value{'id'}");

Better still, why not build the SQL statement up dynamically like this:
Code:
   # Use an array to hold the all column names to be updated (maybe could get
   # this programatically from somewhere...)
   my @cols = ("dateclosed","houseadd","sellfn"); # etc...
   my $sql;
   foreach $col (@cols) {
      if ($sql) {
         $sql .= ", ";
      } else {
         $sql = "UPDATE closings SET ";
      }

      $sql .= "$col=" . $dbh->quote(param($col));
   }
   $sql .= " WHERE id=" . $dbh->quote(param("id"));

   $sth=$dbh->do($sql) or bail_out("Can't update closing");
   view_closings();




-- Chris Hunt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top