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

I have a form that has 4 fields that are of the same format. When I submit the form, I'd like to do the same data validation on all 4. All for are named similarly (i.e. Name1, Name2, Name3, Name4). In my cgi, I'd like to loop over them using the param() call. Is it possible to do something like:
my $name = ""; for(my $num = 1; $num < 5; $num++){ $name = "Name".$num; print param($name); }
If not, any ideas? Note: JavaScript is out of the question for data validation.

Replies are listed 'Best First'.
Re: using variables to get data from CGI.pm's param() function
by The Mad Hatter (Priest) on Apr 07, 2003 at 20:39 UTC
    Yes, that should work as you expect.
Re: using variables to get data from CGI.pm's param() function
by Avox (Sexton) on Apr 07, 2003 at 20:39 UTC
    I withdraw my question. I've discovered the solution. *don't you just hate that!*
      What folks might hate about it is that you didn't give any clue about the solution that you found. This particular sort of question does come up a lot, and usually gets an answer like "yes, that's one way to do it, but there are better ways..."

      If you found a better way, don't be shy -- sharing it is bound to help someone else who's about to ask the same question again.

        Ack, sorry graff, you are completely correct. The solution is that it does work as you'd expect. I found a stupid little error that was causing the whole script to fail. Should have spent another 5 minutes debugging before writing this one up. I'm under the gun and then jumped the gun. :( Thanks for the reply.

      Let's guess:

      foreach my $numbered_param ( grep /^prefix(\d+)/ , param() ) { # do something with $numbered_param }
      ?

      If not P, what? Q maybe?
      "Sidney Morgenbesser"