Best way to write TWO if conditions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Best way to write TWO if conditions
# 1  
Old 08-21-2013
IBM Best way to write TWO if conditions

HI all,
In PERL, I want to know which is best practice among the two methods to write TWO IF conditions.
In first method both conditions are evaluated every time $iop is fetched. but in the second method the second condition ($iop =~ /nop/) is evaluated only if First condition is satisfied ( Am i right? ). Wont the 2nd method save some time or is it not the best practice for large <TST> files?

OR Does the best practice means writing the code in very few lines which is simple to read & maintain.

First method:
Code:
 open (TST, '<c0.ex4.t0.tst');
    abc: while ( $iop = <TST>)
    {
      if( ( $iop =~ /$ra/ ) && ($iop =~ /nop/) )
	      {
		  print "$iop \n";  last abc;
	      }
	
      
    }


second method:
Code:
 open (TST, '<c0.ex4.t0.tst');
    abc: while ( $iop = <TST>)
    {
      if( $iop =~ /$ra/ )
	{
	    if ($iop =~ /nop/)
	      {
		  print "$iop \n";  last abc;
               }
	}
      
    }


Last edited by twistedpair; 08-21-2013 at 07:26 AM..
# 2  
Old 08-21-2013
Even in the first method, if the first condition returns false then the second condition is not evaluated due to the logical and operation.

If your requirement is to satisfy two conditions then the first one is a better approach. In case you need to do some other operation after confirming that first condition is satisfied (other than checking second condition) then you may go for the second approach.

Last edited by krishmaths; 08-21-2013 at 07:14 AM..
This User Gave Thanks to krishmaths For This Post:
# 3  
Old 08-21-2013
IBM

Thanks for reply. after reading your reply to recall logical AND i searched on internet about it. I have found the following link in support of your reply that 2nd condition gets evaluated only if 1st condition is true in logical AND.
http://publib.boulder.ibm.com/infoce...%2Flogande.htm

For my purpose 1st method is best.

Many thanks.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Interactive Python 3.5+ sys.stdout.write() AND sys.stderr.write() bug?

(Apologies for any typos.) OSX 10.12.3 AND Windows 10. This is for the serious Python experts on at least 3.5.x and above... In script format sys.stdout.write() AND sys.stderr.write() seems to work correctly. Have I found a serious bug in the interactive sys.stdout.write() AND... (2 Replies)
Discussion started by: wisecracker
2 Replies

2. Shell Programming and Scripting

Is it possible to write write multiple cronjobs in shellscript??

Hi All, I need the answer of below question? 1) How to write multiple cronjobs in shellscript? Is there any way or we cant write in shellscript... Regards, Priyanka (2 Replies)
Discussion started by: pspriyanka
2 Replies

3. Shell Programming and Scripting

Conditions in if

I'm using the below one.. #!/bin/ksh File=$3 if ; then echo "Script" elif ] ;then echo "Passed k or f option" else "Please check the Input passed" fi Command line argument is "k" or -f and file is exist then... (3 Replies)
Discussion started by: Roozo
3 Replies

4. Shell Programming and Scripting

Errors in if conditions with to many OR conditions

Hi ALL I have a script where in i need to check for several values in if conditons but when i execute the script it throws error such as "TOO MANY ARGUMENTS" if then msg="BM VAR Issue :: bmaRequestVAR=$bmaRequestVAR , nltBMVAR=$nltBMVAR , bmaResponseVAR=$bmaResponseVAR ,... (10 Replies)
Discussion started by: nikhil jain
10 Replies

5. Shell Programming and Scripting

IF OR with two conditions

I have this IF working fine, testing if a char is a digit: if ; then _VALUE=$_VALUE$_CHAR else _ISDIGIT="false" fi Then I add a second condition to test if the char is either a digit or a * if ]; then _VALUE=$_VALUE$_CHAR ... (11 Replies)
Discussion started by: Flavius
11 Replies

6. Shell Programming and Scripting

While with three conditions

Currently this is what I am trying while || && ]; do I want to continue if the first condition or both the second and third are true but I am getting a too many arguments error. Can someone help me out? (5 Replies)
Discussion started by: whdr02
5 Replies

7. IP Networking

read/write,write/write lock with smbclient fails

Hi, We have smb client running on two of the linux boxes and smb server on another linux system. During a backup operation which uses smb, read of a file was allowed while write to the same file was going on.Also simultaneous writes to the same file were allowed.Following are the settings in the... (1 Reply)
Discussion started by: swatidas11
1 Replies

8. Shell Programming and Scripting

conditions

./script 89 The script will extract the last digit of the input parameter. example, that is 4. This will be compared to the last digit of the current day of the month ( like day 14; that is 4). A message will displayed on the screen indicating if the digits are the same or not. (1 Reply)
Discussion started by: singh is king
1 Replies

9. UNIX for Dummies Questions & Answers

2 or more if conditions

Hello, I have a file as follows: col no:1 2 3 4 5 6 7 8 9 10 11 a 4 226 226 ch:95024048-95027592, 1y224 of 3545 223 224 ident b 53 235 235 ch:148398-148401255, 1y184 of 3187 180 186 ident awk... (3 Replies)
Discussion started by: dr_sabz
3 Replies

10. Shell Programming and Scripting

Find all files with group read OR group write OR user write permission

I need to find all the files that have group Read or Write permission or files that have user write permission. This is what I have so far: find . -exec ls -l {} \; | awk '/-...rw..w./ {print $1 " " $3 " " $4 " " $9}' It shows me all files where group read = true, group write = true... (5 Replies)
Discussion started by: shunter63
5 Replies
Login or Register to Ask a Question