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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

'if' statement error 1

Status
Not open for further replies.

varakal

IS-IT--Management
Mar 18, 2004
114
US
I am having a problem with the following code.

Code:
my $exists = 0;
my @exists = $dbh_src->FetchData();
chomp($exists[0]);
print "Before if stmt: Fetched: $exists[0], userid = $user_id, flag = $exists\n";
$exists = 1 if ('$exists[0]' == '$user_id');
print "After if stmt: Fetched: $exists[0], userid = $user_id, flag = $exists\n";

The result is:
Code:
Before if stmt: Fetched: , userid = EC0010, flag = 0
After if stmt: Fetched: , userid = EC0010, flag = 1

Why would $exists change from '0' to '1' if $exists[0] and $user_id are not matched?
Thanks in advance.
 
The problem that I can see from your code is that you're trying to match two literal strings against each other as numbers. Change this line:

Code:
$exists = 1 if ('$exists[0]' == '$user_id');

to:

Code:
$exists = 1 if ("$exists[0]" eq "$user_id");

- Rieekan
 
Thanks Reeikan for your answer. It worked. I tried 'eq' before, but I had single quotes instead of double quotes in the 'if' statement, and with single quotes, it didn't work.
 
Yeah, Perl doesn't evaluate variables within single quotes, so your if statement was checking to see if the literal string $exists[0] equals the literal sting $user_id.

- Rieekan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top