Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

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

by Anonymous Monk
on Oct 06, 2000 at 01:21 UTC ( [id://35484]=perlquestion: print w/replies, xml ) Need Help??

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

[This was moved from the Q&A section due to some confusion on the part of the Q&A editor and some general coding faults better discussed in SoPW]

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.

Replies are listed 'Best First'.
(ar0n) 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 01:24 UTC
    (The following is the answer to what I understand your question to be :)

    How about:
    use strict; foreach ($query->param) { get_cand_info($_) if /^c\d+$/; } sub get_cand_info() { my $cd = shift; my $value = $query->param($cd); # blah }
    param() returns the names of all the parameters when called without any arguments. See the CGI docs.

    update

    Since nobody seems to bring it up,
    $pq_out = `/opt/bin/pq -o '%last:%first:%loc:%room:%tel:%id' hrid=$c1` +;
    is rather unsafe. Read perlsec!


    [ar0n]

Re: How do I pass a sequence of variable names to a subroutine one at a time?
by Russ (Deacon) on Oct 06, 2000 at 05:36 UTC
    @cand_info = map {get_cand_info($query->param($_))} grep {/^c/} $query +->param();
    This will find all CGI parameters which begin with the letter 'c', pass their values to your subroutine one-at-a-time, and result in @cand_info holding all return values from those subroutine calls.

    Did I understand the question?

    Russ
    Brainbench 'Most Valuable Professional' for Perl

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 01:24 UTC
    If I get your question right, you overwrite the @cand_info array each time you call the sub.

    Try this in your for loop:
    push @cand_info, &get_cand_info( $var );
    for each of your variables.
Re: How do I pass a sequence of variable names to a subroutine one at a time?
by wardk (Deacon) on Oct 06, 2000 at 03:58 UTC
    are you just wanting to pass a list to the subroutine?
    # just change these to get $query->param("something"); my $c1 = "foo"; my $c2 = "fee"; my $c3 = "fuu"; doThis($c1, $c2, $c3); sub doThis { foreach (@_ ) { print; } }
Re: How do I pass a sequence of variable names to a subroutine one at a time?
by wardk (Deacon) on Oct 06, 2000 at 04:23 UTC

    Are you attempting to just pass a list of variables all at once?

    If so, just stack them up and they arrive to the subroutine as a simple @array. you can then loop inside the subroutine.

    # these vars could come from a cgi form my $c1 = "foo"; my $c2 = "fee"; my $c3 = "fuu"; doThis($c1, $c2, $c3); sub doThis { foreach (@_ ) { print; } }

    Update: My original reply didn't show up at first, so I redid the darn this, now I have two...apologies

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (7)
As of 2024-04-25 08:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found