Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

$query->param to array checking issue

Status
Not open for further replies.

MJAZ

Programmer
Joined
Aug 1, 2006
Messages
73
Location
US
My little CGI script is acting up again. This time, when it is supposed to check the value of a query (in this case, ?content=about) against an array of possible values, it fails by printing out the error page that is is supposed to do only when the value of the parameter "content" does not equal any of the values in the array. In this case, I know that the value supplied is okay, because I ran it on the command line.

Problematic code (I changed the error page to a one-line string):

Code:
#!C:/Perl/bin/perl.exe

use CGI;
use HTML::Template;
use File::Slurp;
use strict;

my $query = new CGI;
my $content = $query->param( "content" );

$ENV{DOCUMENT_ROOT} = "C:/Program Files/Apache Software Foundation/Apache2.2";


my @contentvalue = ( "login", "passfnd", "signup", "services", "compare", "capabilities",
                      "sc", "wc", "wow", "halo", "halo2", "gwars", "moreg", "violation",
                      "billing", "other", "about", "history" );
                      
unless ( $content eq @contentvalue ) { #the problem line
        print "Error: Bad value.\n\n"
        exit;
}

Any ideas on how to fix?
 
Try replacing this line:
Code:
unless ( $content eq @contentvalue ) { #the problem line

with this one:
Code:
unless( grep { $_ eq $content} @contentvalue ) {

Let us know your results!

X
 
or use a hash:

Code:
my %contentvalue = (
   login => 1,
   passfnd => 1,
   signup => 1,
   services => 1,
   compare => 1,
   capabilities => 1,
   sc => 1,
   wc => 1,
   wow => 1,
   halo => 1,
   halo2 => 1,
   gwars => 1,
   moreg => 1,
   violation => 1,
   billing => 1,
   other => 1,
   about => 1,
   history => 1,
);

unless (exists $contentvalue{$content}) { #the problem line
        print "Error: Bad value.\n\n"
        exit;
}

 
Thanks guys. Xaqte's one worked alright.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top