in reply to There's got to be a better way?

What I don't understand is why you need this code at all. Several people suggested alternatives involving hashes and arrays and for loops and so on, but it seems to me that that misses the point, and that these six lines are completely unnecessary. From the way you're using $FORM{'h04'}, I can see that it is basically a boolean variable, with a true value if a certain box was checked, and a false value if not. What's the point of creating a second variable to record exactly the same information?

For example, if you're planning to test the $checked variables later, like this:

if ($checked03) { ... }
then you can just do this instead:
if ($FORM{'h03'}) { ... }
If you're planning to print out the word CHECKED then why not put that logic in the place where the message is printed, instead of creating six separate variables beforehand?

--
Mark Dominus
Perl Paraphernalia

Replies are listed 'Best First'.
Re: Re: There's got to be a better way?
by Anonymous Monk on May 20, 2001 at 07:52 UTC
    Printing the word CHECKED is exactly what I want to do. I find it to be simpler to have the variables set in advance as opposed to incorporating them into my print statements which would then require an alternative else statement. Also, dealing with them before hand makes reusing the code in another application down the road a whole lot easier.
        ++, good solution. A minor change though, I often find it nicer with a printed text for both situations, i.e:
        print "Box 4: ", $FORM{'h04'} ? 'CHECKED' : 'NOT CHECKED';


        The 15 year old, freshman programmer,
        Stephen Rawls
      Printing the word CHECKED is exactly what I want to do. I find it to be simpler to have the variables set in advance as opposed to incorporating them into my print statements which would then require an alternative else statement.
      If you arrange things correctly, it will all work out right---which was what you were asking for. Change your HTML form to contain HTML code like this:
      <input type=checkbox name='h04' value=' CHECKED '>
      Now when the user checks the checkbox, $FORM{'h04'} will automatically contain the value you want, CHECKED. Then to print out your report, you just say:

      print "Box 4: $FORM{h04}";
      You no longer need the extra variables, because the form variables get the correct values in the first place. No else required.

      --
      Mark Dominus
      Perl Paraphernalia