perl : semi complex if statement question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl : semi complex if statement question
# 1  
Old 11-17-2009
perl : semi complex if statement question

Hi there

I am trying to write an if statement in perl that will return "SUCCESS" if either of these conditions are true

a) if $changes is greater than 5 AND the $force flag is set to 1

OR

b) if $changes is greater than 0 AND $changes is less than 6


Code:
#!/usr/bin/perl -w

my $force = "0";
my $changes = "7";


if ((($changes gt "5") && ($force = "1")) || (($changes gt "0") && ($changes lt "6"))) {
        print "success\n";
} else {
        print "failure\n";
}

My script seems to be syntactically correct, however, whatever i set the $changes and $force values to, it always reports "success" .... is there something im doing wrong here ??

any help would be great
# 2  
Old 11-17-2009
try using eq instead of =
# 3  
Old 11-17-2009
thanks

---------- Post updated at 04:56 PM ---------- Previous update was at 07:47 AM ----------

actually, sorry, i replied a bit prematurely . it still doesnt work ... im still getting success when I go over the value "9" on $changes, ill give an example. Although your suggestion does at least make the statement work up to the value 9, but not beyond ...

Code:
#!/usr/bin/perl -w

my $force = "0";
my $changes = "9";

if ((( $changes gt "5" ) && ( $force eq "1" )) || (( $changes gt "0" ) && ( $changes lt "6" ))) {
        print "success\n";
} else {
        print "failure\n";
}

Code:
# test.pl
failure
#

but now when i switch the value of $changes to "10", it for some reason returns true

Code:
#!/usr/bin/perl -w

my $force = "0";
my $changes = "10";

if ((( $changes gt "5" ) && ( $force eq "1" )) || (( $changes gt "0" ) && ( $changes lt "6" ))) {
        print "success\n";
} else {
        print "failure\n";
}

Code:
# test.pl
success
#

Could anybody explain to me whats going on here ?, its driving me crazy Smilie
# 4  
Old 11-17-2009
Hi, you are using string values and string operators instead of numerical operators. Try:
Code:
#!/usr/bin/perl -w

my $force = 0;
my $changes = 10 ;

if ((( $changes > 5 ) && ( $force == 1 )) || (( $changes > 0 ) && ( $changes < 6 ))) {
  print "success\n";
} else {
  print "failure\n";
}

# 5  
Old 11-17-2009
Quote:
Originally Posted by rethink
... your suggestion does at least make the statement work up to the value 9, but not beyond ...
In addition to Scrutinizer's response, you may want to recall that string comparison is ASCII-value comparison.
Which is why these scripts behave the way they do:

Code:
$
$ perl -le '$x="9"; if ($x gt "5") {print "$x gt 5"} else {print "$x le 5"}'
9 gt 5
$
$ perl -le '$x="10"; if ($x gt "5") {print "$x gt 5"} else {print "$x le 5"}'
10 le 5
$
$

tyler_durden
# 6  
Old 11-19-2009
thanks Guys, my logic was that if you had operators like "lt" "gt" "eq" etc, it would be logical to use those ones for numerical comparisons and the "=" etc for strings .... but i guess the perl guys didnt agree

thanks again
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Help with understanding this regex in a Perl script parsing a 'complex' string

Hi, I need some guidance with understanding this Perl script below. I am not the author of the script and the author has not leave any documentation. I supposed it is meant to be 'easy' if you're a Perl or regex guru. I am having problem understanding what regex to use :confused: The script does... (3 Replies)
Discussion started by: newbie_01
3 Replies

2. Shell Programming and Scripting

Complex Regex Perl

Hi the below perl snippet will replace any three letter string in the beginning with a two letter string which is specified..but if i want to modfiy only certain characters for eg.. ABC - AB CAB - AB AAA - No Modifcations 1AB - AB AB8 - AB Whatever coming before or after of AB only have... (2 Replies)
Discussion started by: rajkrishna89
2 Replies

3. Shell Programming and Scripting

A question about if statement

Hi everybody, I'm sorry If I ask a silly question. I have a simple code like this I have the following error: Can anyone explain for me why I have this error, and how can I correct it? Thanks in advance. (12 Replies)
Discussion started by: Dark2Bright
12 Replies

4. Shell Programming and Scripting

complex Awk Question

Hi, I have a file look likes this : --->start hir Trace file: pudwh_ora_9998.trc Sort options: fchela exeela ***************************************************************count = number of times OCI procedure was executed cpu = cpu time in seconds executing elapsed = elapsed... (3 Replies)
Discussion started by: yoavbe
3 Replies

5. UNIX for Dummies Questions & Answers

Help with Perl If statement

Hi All, I am writing a perl script where I take 2 variables from the user as STDIN to scan the lines of a file to produce as output. How can I do an IF loop to test this for example in the mock file 12 10 35 20 37 5 45 12if I take user input as 40 and 10, how can I get the output lines in... (3 Replies)
Discussion started by: pawannoel
3 Replies

6. Shell Programming and Scripting

A complex sed statement

I have following requirement. Say, my text file contains following patterns {2010501005|XXGpvertex|9|0|17|0|{|{30100001|XXparameter_set|@@@@{{30001002|XXparameter|!prototype_path|$AB_COMPONENTS/Sort/Sort.mpc|3|2|Pf$|@{0|}} }}@0|@315000|78500|335000|99000|114000|87000|17|And the Sort|Ab... (8 Replies)
Discussion started by: Shell_Learner
8 Replies

7. Shell Programming and Scripting

complex if statement syntax without using 'if ..' keyword in ksh.

It saves me lot of typing and space/lines when I do not use full 'if' keyword and construct, instead use .. && <statement> || <statement> that perfectly replaces.. if ; then <statement> else <statement> fi Can I use following syntax when I want to add multiple statements under 'if'... (4 Replies)
Discussion started by: kchinnam
4 Replies

8. UNIX for Dummies Questions & Answers

Complex If statement

can anyone please explain my below statement, i am confused. if || \ || \ || \ || then (1 Reply)
Discussion started by: kd09714
1 Replies

9. Shell Programming and Scripting

IF statement question

Hi there We have boxes named server-sybase2, server-oracle1, etc etc Does any body know how I can construct an if statement to say, if the hostname of the box contains the string "sybase" then do X ie if then X fi any help would be greatly appreciated (6 Replies)
Discussion started by: hcclnoodles
6 Replies

10. Shell Programming and Scripting

Complex Sed/Awk Question?

Hello, So i have this file called /apps/turnout which looks like that of the contents of the /etc/shadow (but not exactly) the file has a long list in it. basically, the contents of this file looks something similar to the following: jajajajalala:D#$#AFVAdfda lalabavisof:#%R@fafla#$... (3 Replies)
Discussion started by: SkySmart
3 Replies
Login or Register to Ask a Question