CGI & looping

Alan Stewart astewart at spawar.navy.mil
Sun Jul 16 13:59:49 CDT 2000


~sdpm~
On 16 Jul 00, at 10:30, Joel Fentin wrote:

> ~sdpm~
> >~sdpm~
> >for one thing you don't need to specify the iterator $_ in the
> foreach loop.
> >it's used by default. and i dont think you can use a foreach as
> a paramter to
> >the print statement.
> >try using join {map} to print the data list if it has to be a
> paramter, or
> >otherwise, print it separately.
> 
> In spite of the subject of the email, and in spite of my first
> sentence, you talked to me about printing. Nobody answered the
> looping issues.
> 
> Those loops work fine and print fine when not used with CGI.
> Again, I ask: How do I loop?

Well, Jeff did answer your question, if very briefly. The magic loop word is 
"map". On the other hand, your problem IS with the print statement (not with 
CGI). Notice that in your example the foreach statement is preceded and 
followed by a comma. That means that it is trying to be a "parameter" in a list 
to the print statement and that's no good. You can use a single function that 
returns a single value or a list (like map), but the foreach code doesn't do 
that. You have (at least) two choices:

Using map (and a join is not needed):

#!/perl/bin/perl -w
use CGI;
my $co = new CGI;
my @Data=("AAAA","BBBB","CCCC","DDDD");
print $co->header,
$co->start_html(-title=>'Hello Mother'),
$co->start_form(),
$co->p, at Data,
map {$co->p,"$_\n"} @Data,
$co->end_form(),
$co->end_html;

This works if the loop is a simple one to one conversion. If you want to do 
arbitrary stuff in the loop, you need to break up the print statement:

#!/perl/bin/perl -w
use CGI;
my $co = new CGI;
my @Data=("AAAA","BBBB","CCCC","DDDD");
print $co->header,
$co->start_html(-title=>'Hello Mother'),
$co->start_form(),
$co->p, at Data; # end this print
foreach (@Data){print $co->p,"$_\n"} # print "inside" some arbitrary code
print $co->end_form(), # finish printing
$co->end_html;

or stick a supporting subroutine in:

#!/perl/bin/perl -w
use CGI;
my $co = new CGI;
my @Data=("AAAA","BBBB","CCCC","DDDD");
print $co->header,
$co->start_html(-title=>'Hello Mother'),
$co->start_form(),
$co->p, at Data,
munge_stuff(@Data),
$co->end_form(),
$co->end_html;

sub munge_stuff {
    my @array = @_;
    # do whatever you need to do
    return @array;
}

All of this is just print syntax, not CGI, which is the $co->xxx stuff.


-------------------------------------------------------------
 Alan Stewart          )-[]-(           Electronics Engineer
 Code D621           ~        ~         Network Operations
 SPAWARSYSCEN       ~          ~  \     Satellite Communications
 53560 Hull St   ( ~            ~  )    tel (619)524-3625
 San Diego,CA  __|___             /|    fax (619)524-2607
 92152-5001   ^\____/^^^^^^\    __| |_  astewart at spawar.navy.mil
-------------^^^^^^^^^^^^^^^\__|______|_-------------------------
~sdpm~

The posting address is: san-diego-pm-list at hfb.pm.org

List requests should be sent to: majordomo at hfb.pm.org

If you ever want to remove yourself from this mailing list,
you can send mail to <majordomo at happyfunball.pm.org> with the following
command in the body of your email message:

    unsubscribe san-diego-pm-list

If you ever need to get in contact with the owner of the list,
(if you have trouble unsubscribing, or have questions about the
list itself) send email to <owner-san-diego-pm-list at happyfunball.pm.org> .
This is the general rule for most mailing lists when you need
to contact a human.




More information about the San-Diego-pm mailing list