×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Comparing 3 vars if defined.

Comparing 3 vars if defined.

Comparing 3 vars if defined.

(OP)
I have a scripting problem .. not sure where to start.

I will have 3 vars. They could or could not be defined. For any defined var, I need to check that the string is identical to other define vars.

So for example I could have :
$x=NOW IS THE TIME
$y=NOW IS THE TIME
$z=NOW IS THE TIME

OR
$x=NOW IS THE TIME
$y=
$z=Now ISnt THE TIME

or

$x=NOW IS THE TIME
$y=
$z=

or any other combination thinkable.

Thanks in advance

RE: Comparing 3 vars if defined.

Here is a simple solution but I expect there are others.

CODE

if(($x eq $y) && ($x eq $z)){
# They match
} 

Keith
www.studiosoft.co.uk

RE: Comparing 3 vars if defined.

(OP)

Thank Keith .. sheez .. now that I see it, it looks too easy ... should have been able to figure that out. Guess sometimes I just become blind ... cant "see" it ... thanks again.

RE: Comparing 3 vars if defined.

The problem with audiopro's solution is that it only works when all three values are defined. As I understand it, you want to pick cases where all defined values are identical, and ignore undefined values. So the first and last of the three cases you listed should pass, whilst the middle one should fail.

I haven't tested it, but something like this should do:

CODE

if ((($x eq $y) || ($x eq '') || ($y eq ''))
 && (($x eq $z) || ($x eq '') || ($z eq ''))
 && (($y eq $z) || ($y eq '') || ($z eq ''))) then
   # They match
} 

-- Chris Hunt
Webmaster & Tragedian
Extra Connections Ltd

RE: Comparing 3 vars if defined.

I assumed that the vars would be declared at the start of the script.
My latest offering would be.

CODE

my $x='';
my $y='';
my $z='';
if(($x ne '') && ($x) && ($y) && ($z) && ($x eq $y) && ($x eq $z)){
# They match
} 

Keith
www.studiosoft.co.uk

RE: Comparing 3 vars if defined.

This should give you what you desire.

CODE

my @defined_vars = grep {defined $_} ($x, $y, $z);

if (@defined_vars && ! grep {$_ ne $defined_vars[0]} @defined_vars) {
    print "All Vars equal each other if they are defined\n";
} 

The only special case that you didn't specify is what should happen if none of them are defined.

- Miller

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close