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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dynamic/concatenate variable 1

Status
Not open for further replies.

egrant

Programmer
Mar 6, 2003
42
0
0
AU
Hi All,

I have been researching on this forever and can't find an answer.

I need to rename my variable depending on what my counter value is for each total in my query array. For instance I would like a unique Total for each record so I can refer to all the records once I am out of the array. Something like;

while (@RowDetails = $RowDetails_sth->fetchrow_array())
{
$Counter ++
$Total_$Counter = $RowDetails[0]
}
...

print $Total_1
print $Total_2
print $Total_3
etc...

This is not working for me and I totally lost on how to do it. Any help or pointing in the right direction would be much appreciated!

Thanks,

E
 
I know I've seen this answered a few times in this forum, but I couldn't find a specific post to answer your question. The short answer is, you probably don't want to use dynamic variable naming. Take a look at either using an array or a hash.

Try something like:

Code:
my %totals;
while (@RowDetails = $RowDetails_sth->fetchrow_array())
{
   $Counter ++;
   $totals{$Counter} = $RowDetails[0];
}

And when you want to print the totals:
Code:
# Prints all the totals
foreach (keys %totals) {
  print "Total $_ - $totals{$_}\n";
}

# Prints just total 2 - instead of using $total_2
print $totals{2};
 
Thanks!

Worked like a charm. I understand hash alot better now too!

Thanks Again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top