Newbie Need help with if statement

E

Ed

I need help with the if statement below. What I need instead of three if
statements is one if and two "or" or something similar. Pseudo code would be

if
$areacode is blank or
$exchange is blank or
$phone is blank
then
$er_phone = $er_message
$er_count = $er_count + 1

T.I.A.
Ed
----------------------------------------------------


if ($areacode eq "") {
$er_areacode = $er_message;
$er_count = $er_count + 1
}
if ($exchange eq "") {
$er_exchange = $er_message;
$er_count = $er_count + 1
}
if ($phone eq "") {
$er_phone = $er_message;
$er_count = $er_count + 1
}
 
B

Ben Morrow

Ed said:
I need help with the if statement below. What I need instead of three if
statements is one if and two "or" or something similar. Pseudo code would be

if
$areacode is blank or
$exchange is blank or
$phone is blank
then
$er_phone = $er_message
$er_count = $er_count + 1

and... what did you try?

if ($areacode eq "" or
$exchange eq "" or
$phone eq ""
) {
$er_phone = $er_message;
$er_count++;
}

Read perldoc perlop before you post again.

Ben
 
P

Paul Lalli

Date: Mon, 1 Mar 2004 08:11:46 -0700
From: Ed <[email protected]>
Newsgroups: comp.lang.perl.misc
Subject: Newbie Need help with if statement

I need help with the if statement below. What I need instead of three if
statements is one if and two "or" or something similar. Pseudo code would be

if
$areacode is blank or
$exchange is blank or
$phone is blank
then
$er_phone = $er_message
$er_count = $er_count + 1

T.I.A.
Ed
----------------------------------------------------


if ($areacode eq "") {
$er_areacode = $er_message;
$er_count = $er_count + 1
}
if ($exchange eq "") {
$er_exchange = $er_message;
$er_count = $er_count + 1
}
if ($phone eq "") {
$er_phone = $er_message;
$er_count = $er_count + 1
}

You need to read perldoc perlop. Look for the section on logical
operators. The operator you probably want in this case is 'or':

if ($areacode eq '' or $exchange eq '' or $phone eq ''){
$er_phone = $er_message;
$er_count++;
}

Paul Lalli
 
R

Regent

Ed said:
I need help with the if statement below. What I need instead of three if
statements is one if and two "or" or something similar. Pseudo code would be

if
$areacode is blank or
$exchange is blank or
$phone is blank
then
$er_phone = $er_message
$er_count = $er_count + 1

T.I.A.
Ed
----------------------------------------------------


if ($areacode eq "") {
$er_areacode = $er_message;
$er_count = $er_count + 1
}
if ($exchange eq "") {
$er_exchange = $er_message;
$er_count = $er_count + 1
}
if ($phone eq "") {
$er_phone = $er_message;
$er_count = $er_count + 1
}
try this:

if (! ($areacode and $exchange and $phone))
{
$er_phone = $er_message;
$er_count = $er_count + 1;
}

Regent
 
P

Paul Lalli

try this:

if (! ($areacode and $exchange and $phone))
{
$er_phone = $er_message;
$er_count = $er_count + 1;
}

Please note that's not the same as what the OP asked for. This code is
simply checking to see if any of the three variables have false values
(including 0 or "0"), not necessarily that they're blank.

Paul Lalli
 
T

Tore Aursand

I need help with the if statement below. What I need instead of three if
statements is one if and two "or" or something similar. Pseudo code
would be

if
$areacode is blank or
$exchange is blank or
$phone is blank
then
$er_phone = $er_message
$er_count = $er_count + 1

Why didn't you try to implement this pseudo-code? It would have worked,
most probably;

if ( $areacode eq '' or $exchange eq '' or $phone eq '' ) {
$er_phone = $er_message;
$er_count = $er_code + 1; # Can be written as $er_code++
}
 
S

Steven Vasilogianis

Ed said:
I need help with the if statement below. What I need instead of three if
statements is one if and two "or" or something similar. Pseudo code would be

if
$areacode is blank or
$exchange is blank or
$phone is blank
then
$er_phone = $er_message
$er_count = $er_count + 1

Erm, your psuedo-code does not describe what your real code below is
doing.
if ($areacode eq "") {
$er_areacode = $er_message;
$er_count = $er_count + 1
}
if ($exchange eq "") {
$er_exchange = $er_message;
$er_count = $er_count + 1
}
if ($phone eq "") {
$er_phone = $er_message;
$er_count = $er_count + 1
}

So, which is it? Set only $er_phone to $er_message, or set the
corresponding $er_(areacode|exchange|count) to $er_message? If it's
the latter, I think your real code is pretty much as good as it can
get.

HTH,
 
B

Ben Morrow

Steven Vasilogianis said:
Erm, your psuedo-code does not describe what your real code below is
doing.


So, which is it? Set only $er_phone to $er_message, or set the
corresponding $er_(areacode|exchange|count) to $er_message? If it's
the latter, I think your real code is pretty much as good as it can
get.

No, you should be using two hashes:

my %input;
my %er;

# fill %input

for (qw/areacode exchange phone/) {
if ($input{$_} eq '') {
$er{$_} = $er_message;
$er{count}++;
}
}

Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
EmeliaBryc

Latest Threads

Top