Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

How do I pass a sequence of variable names to a subroutine one at a time?

by guzz1 (Novice)
on Oct 06, 2000 at 00:34 UTC ( #35471=perlquestion: print w/replies, xml ) Need Help??

guzz1 has asked for the wisdom of the Perl Monks concerning the following question:

I want to modify this so it executes on each variable passed to it, e.g. $c1, then $c2, $c3, etc. This works for $c1:
$c1 = $query->param( 'c1' ); @cand_info = &get_cand_info( $c1 ); ..... sub get_cand_info() { my ( $c1 ) = @_; my @cand_info = (); my $pq_out; # Gather the information from a POST query. if( length($c1) == 7 ) { push( @cand_info, 'HRID' ); $pq_out = `/opt/bin/pq -o '%last:%first:%loc:%room:%te +l:%id' hrid=$c1`; } chomp $pq_out; # Put the data into the list. @cand_info = ( @cand_info, split(/:/, $pq_out) ); ($cand_info[1] = $cand_info[1]) =~ s/_/ /g; # name massage return @cand_info; }
I've been trying to do this by calling the subroutine in a for...next loop, and passing it a variable which changes value each time through the loop, but with no success:
for ($gi = 1; $gi <= $tno; $gi++ ) { $tnum = "c".$gi; $tnum2 = $query->param( '$tnum' ); @cand_info = &get_cand_info( $tnum2 ); }
I'm worn out with trying different variations on how to pass the value(s) to the subroutine, help would be greatly appreciated.

Originally posted as a Categorized Question.

Re: How do I pass a sequence of variable names to a subroutine one at a time?
by le (Friar) on Oct 06, 2000 at 00:56 UTC
    Try this:
    push @cand_info, &get_cand_info($var);
    for each variable.

    Originally posted as a Categorized Answer.

Re: How do I pass a sequence of variable names to a subroutine one at a time?
by ar0n (Priest) on Oct 06, 2000 at 00:55 UTC
    How about:
    use strict; foreach ($query->param) { do_stuff($_) if /^c\d+$/; } sub do_stuff() { my $cd = shift; my $value = $query->param($cd); # blah }
    param() returns the names of all the parameters when called with any arguments. See the CGI docs.

    Originally posted as a Categorized Answer.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://35471]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2023-01-27 11:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?