[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[MacPerl-WebCGI] Re: [MacPerl-Forum] Putting hash values in an array and checking that with an foreach is not working!



On Mon, 12 Jun 2000 16:07:59 +0200, Jimmy Lantz wrote:

>########## Snippet ###########
>@checks = ('$in{\'kurskod\'}', '$in{\'pnr\'}', '$in{\'fnamn\'}',
>'$in{\'enamn\'}', '$in{\'telnr\'}', '$in{\'padress\'}',
>'$in{\'postnr\'}', '$in{\'ort\'}', '$in{\'studietyp\'}', '$in{\'tentaresultat\'}');
>
>foreach $check (@checks)
>{
>if ($check eq '')
>{
>&do_error;
>exit;
>}
>}

Of course it won't ever generate an error. You're testing the literal
strings above, which are all notempty.

Drop the (single) quotes around each item, and the backslashes in front
of the inner single quotes. Actually, those quotes themselves aren't
really necessary for barewords.

	@checks = ($in{kurskod}, $in{pnr}, $in{fnamn}, ...);

I'd reduces your test to:

    foreach my $key (qw[kurskod pnr fnamn enamn telnr padress
      postnr ort studietyp tentaresultat])
    {
        if ($in{$key} eq '')
        {
	    &do_error;
            exit;
        }
    }

Or even:

    if (grep { $in{$_} eq '' } qw[kurskod pnr fnamn enamn telnr
      padress postnr ort studietyp tentaresultat])
    {
        &do_error;
        exit;
    }

And finally, you could use a hash slice:

    for my $check (@in{qw[kurskod pnr fnamn]}) {
        if ($check eq '') {
            &do_error;
            exit;
	}
    }

-- 
	Bart.

==== Want to unsubscribe from this list?
==== Send mail with body "unsubscribe" to macperl-webcgi-request@macperl.org