Solved

Test for value of an array?

Posted on 2003-11-05
4
401 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now