Solved

Test for value of an array?

Posted on 2003-11-05
4
405 Views
Last Modified: 2010-03-04
Hi All,

I have a cgi form that has several question I want filled out. Question 2 has multpile options and an 'other' text box. These options are listed as checkboxes. Users should can fill out as many as apply but they must at least fill out one checkbox or something in the 'other' input box.

<input type=checkbox name=q2_a value=myvalueA>
<input type=checkbox name=q2_b value=myvalueA>
<input type=checkbox name=q2_c value=myvalueA>
<input type=checkbox name=q2_d value=myvalueA>
<input type=text name=q2_other>

The data processing part of the script is grabs the params in the post and checks to see if there is any values passed from those params like so:

my $q2_a      = param('q2_a');
my $q2_b      = param('q2_b');
my $q2_c      = param('q2_c');
my $q2_d      = param('q2_d');
my $q2_other      = param('q2_other')

I load the variables into an array and do the test figuring that if there is no value in the array then the user must not have answered the question and then send them back.

my @q2test = ($q2_a, $q2_b, $q2_c, $q2_d, $q2_other);

if (@q2test eq "") {
            GetTemplate();
            print "You have left a required field blank. Please select an option from question    2.<br>";
            print "Please use the back button and try again. Thank You.";
            print"<p>Debug ~ Q2 Test: @q2test";
            EndTemplate();
      }
It's weird because if I test it and leave all the checkboxes empty and input box it does not throw the error even though the value of the array is nothing.  If I check something or fill out the text box then it passes fine but that doesn't get my goal accomplished.

Anyone have any thoughts? Is there a better way?  

All I want to do is make sure something is answered here. Kinda stumped and any help would be apreciated.

thanks!!
-mike

0
Comment
Question by:mookie13
  • 3
4 Comments
 
LVL 3

Accepted Solution

by:
cwp earned 75 total points
ID: 9692602
If you use @q2test, it will return the number of elements in the array, so you're really comparing "8" with "".

You can try this instead of the @q2test:
   my $q2 = $q2_a || $q2_b || $q2_c || $q2_d || $q2_other;
   if ($q2 eq "") {
      print "Question 2 field blank\n";
   }

0
 
LVL 3

Expert Comment

by:cwp
ID: 9692609
Correction: If you use @q2test *in a scalar context*. @q2test eq "" is scalar enough.
0
 

Author Comment

by:mookie13
ID: 9696144
Thank you so much CWP!!! It worked like a charm.
0
 
LVL 3

Expert Comment

by:cwp
ID: 9696248
No problem. Not sure if you've noticed, but it's supposed to be 'comparing "5" with ""' as well... I guess 3am isn't a good time for me to answer questions ;).

Anyway, good luck with your program!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Perl script to parse log and delete the file 17 178
hard perl script 16 158
Trying to use two loops in a perl script but not getting results need. 10 93
File Find regex problem 4 64
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question