Click to See Complete Forum and Search --> : Perl Problem


zmcgrew
03-22-2001, 11:04 PM
I have begun recently to learn perl, I'm not a newbie by anymeans, but I'm not an expert yet either.
I have been encountering some problems with the if, else statements though. I using it for CGI, but what does that matter?
Oh well, take in the query string, and then check the value of it. Sounds simple huh?
However, no matter what the value it, it thinks that it equals "home" no matter if it equals "about" or "links"...
Will someone give me an example of a "working" if, elsif, else statement plz?
I mean, I thought that it was:

if (statement) {

}
elsif (statement) {

}
else {

}

Of course statement could be anything though, x == 1, x == 2, name == "bob", etc.

What am I doing wrong?
:confused:

Whipping Boy
03-22-2001, 11:27 PM
Just curious--are you evaluating the value of QUERY_STRING using "==" or "="?

crokett
03-23-2001, 12:02 AM
here ya go.

# demonstrates an if loop

$NUM=0;
$NUM2=0;

print "Please enter a number: \n";
$NUM=<STDIN>;
print "Please enter another number: \n";
$NUM2=<STDIN>;

if ($NUM==$NUM2){
print "Numebers are equal.\n";
}
elsif ($NUM>$NUM2){
print "The first number is larger.\n";
}
else {
print "The second number is larger.\n";
}

YaRness
03-23-2001, 09:53 AM
if you are evaluating a string you want to use "eq" or a regular expression

my $string = 'foo';
if ($string eq 'foo') {print "yes"}
if ($string =~ /foo/) {print "yes"}

you have the if/elseif/else structure correct, maybe it's just your expression that isn't right.

zmcgrew
03-23-2001, 07:03 PM
I was actually dumping QUERY_STRING into an array, and spliting a couple of times =)

So it'd end up like this:

My array was:

*.pl?place=links

(Just an example of one of my values)

@query[0] was place
@query[1] was the content

if (@query[1] == "about") {
#Insert my code here
}
elsif (@query[1] == "links") {
#Insert my code here
}
else {
#Insert my code here
}

It keeps sticking on the first one, and never goes any farther...
What am I doing wrong?

Energon
03-23-2001, 08:31 PM
as stated above, you compare strings with eq, not ==... like this:

if($var eq "whatever")

not

if($var == "whatever")

zmcgrew
03-23-2001, 09:42 PM
Ok, thnx soooo much!
It worx now! Woo hoo!
BTW, what I was doing was creating a developer web page. =)
I develop and write appplications and games for fun. I am also in the process of creating a developer section!
The dev section is kinda like sensei's NHF's but they're ALL about programming!
I don't have any up yet, but when I get the whole site finsihed, I'll give out the link, if you want to go ahead and book mark it, it's: http://izzzy.0host.net

That's a 0 as in a number, not O as in the letter for those wandering.
Again, thnx soooooooo much! :)