[thelist] Perl loop -1

David Bindel dbindel at austin.rr.com
Thu Feb 27 23:47:01 CST 2003


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

> -----Original Message-----
> From: thelist-admin at lists.evolt.org
> [mailto:thelist-admin at lists.evolt.org] On Behalf Of Hugh Blair
> Sent: Thursday, February 27, 2003 11:31 PM
> To: thelist at lists.evolt.org
> Subject: [thelist] Perl loop -1
>
> Again my Perl skills (-x) are showing. What is the way to
> change the loop below to cycle through $field and print all
> of them *except* the last one?
>
> ################################
>   foreach $field (@Field_Order) {
>      print "$field: $Form{$field}<br />\n";
>   }
> ################################
>
> Obviously this is a loop to read a form page. I just
> don't want the final value which is the "Submit" button.

I've never used Perl before (I know it's sad), but I am an avid
server-side programmer in PHP, and your Perl code looks almost
identical to PHP.

A more logical solution would be to just get rid of the value for the
submit button.  How?  Remove the name attribute from the button's
HTML tag like this:

<input type="submit" name="submit" value="Submit" />
becomes
<input type="submit" value="Submit" />

By removing the name attribute, the submit button has no name/value
pair, so it isn't sent with the rest of the named form elements to
the script.

Another alternative would be to use a for loop instead of foreach (I
shy away from foreach loops).

Example (guesswork, I'm not 100% sure if it works in Perl, but it's
my honest attempt):

for ($x = 0; $x <= sizeof($field) - 1; $x++)
	print "$field: $Form($field)<br />\n";


HTH,
David Bindel

-----BEGIN PGP SIGNATURE-----
Version: PGP 7.0.4

iQA/AwUBPl737X6TtTuKuQdTEQIv1ACdFohR8JdFasgtNgr5bT5q3PUBdgEAoLAh
wZUrhidStul9KW7fvkb1Wi0W
=vG7c
-----END PGP SIGNATURE-----




More information about the thelist mailing list