perl q's

D. Sanderson dsanders at u.washington.edu
Sun Mar 12 18:14:45 PST 2000


And if you really want the variables' naems to be $name1, $name2, and so
forth:

#!/usr/bin/perl

%param = ('temp1', 'Hello',
          'temp2', 'Goodbye',
          'temp3', 'Super',
          'temp4', 'Duper');
$count = 4;

for ($a = 1; $a <= $count; $a++) {
    eval('$temp' . $a . ' = ' . $param{'temp' . $a} . ';');
    print '$temp'.$a.' = '.$param{'temp'.$a}.';'."\n";
}

print $temp1, ", ", $temp2, ", ", $temp3, ", ", $temp4, "\n";


On Sun, 12 Mar 2000, C. Olmsted wrote:

> Hey all,
> 
> Yet another non-linux question :)  I'm working on a cgi development for
> work.  I have a list of input variables on an html form set up like this:
> 
> name1, name2, name3, name4,...
> 
> I would like to create a loop to parse each of the names in turn since
> there could be any arbitrary number of these variables with
> different values for each (the form is dynamic).
> 
> My thinking is something like this...
> 
> for ($i = 1; $i <= $count; $i++)
> {
> 	$name$i = param('$name$i')
> 	print $name$i;
> }
> 
> where $count is the number of names that exist in the list.
> 
> Of course this doesn't work since I don't know how to combine
> two variables into one variable name.  Is a foreach loop what I really
> want to use?
> 
> Thanks,
> Cliff
> 
> 
> 

_____________________________________________________________________
Dan Sanderson                               dsanders at u.washington.edu
University of Washington      http://students.washington.edu/dsanders



More information about the Linux mailing list