Coding Bash vs. Perl vs. Python (by Evan Liu)


Topics about flow control

Flow control: for clause


		  
		  

Tasks:
1.1. for statement example
2.2. oneliner
3.3. other alternatives
		  
Bash Perl Python
  1. #1.1. for statement example
  2. for i in {1..10}; do
  3. echo -n "$i "
  4. done
  5. echo
  6. #2.2. oneliner
  7. for i in {1..10}; do echo -n "$i "; done; echo
  8. #3.3. other alternatives
  9. for ((i=1; i<10; i++)); do echo -n "$i "; done; echo
  10. -------output----------
  1. #1.1. for statement example
  2. 1 2 3 4 5 6 7 8 9 10
  3. #2.2. oneliner
  4. 1 2 3 4 5 6 7 8 9 10
  5. #3.3. other alternatives
  6. 1 2 3 4 5 6 7 8 9
  1. #1.1. for statement example
  2. foreach $d (1..10) {\
  3. print "$d ";}
  4. #2.2. oneliner
  5. print "$_ " foreach (1..10)
  6. -------output----------
  1. #1.1. for statement example
  2. 1 2 3 4 5 6 7 8 9 10 #2.2. oneliner
  3. 1 2 3 4 5 6 7 8 9 10
  1. #1.1. for statement example
  2. for i in xrange(1,11):
  3. print i,
  4. else:
  5. print
  6. #2.2. oneliner
  7. for i in xrange(1,11): print i,
  8. # in perl 3, print is a function use
  9. # for i in xrange(1,11): print(i, end=" ")
  10. -------output----------
  1. #1.1. for statement example
  2. 1 2 3 4 5 6 7 8 9 10
  3. #2.2. oneliner
  4. 1 2 3 4 5 6 7 8 9 10 # in perl 3, print is a function use
  5. # for i in xrange(1,11): print(i, end=" ")

Flow control: while clause


		  
		  

Tasks:
1.1. read user input in while statement example
2.2. read user input in while statement with assignmnet in conditional
		  
Bash Perl Python
  1. #1.1. read user input in while statement example
  2. while true; do
  3. read -p "quit? yes/no:" a
  4. [[ "$a" == 'yes' ]] && break
  5. done
  6. #2.2. read user input in while statement with assignmnet in conditional
  7. while read -p "quit? yes/no:" a; do
  8. [[ "$a" == 'yes' ]] && break
  9. done
  10. ------------output--------------------
  1. [eml@ubuntu ~]$
  2. while true; do
  3. > read -p "quit? yes/no:" a
  4. > [[ "$a" == 'yes' ]] && break
  5. > done
  6. quit? yes/no:
  7. quit? yes/no:no
  8. quit? yes/no:yes
  9. [eml@ubuntu ~]$
  1. #1.1. read user input in while statement example
  2. while (true){ \
  3. my $a=<>; \
  4. print "quit?yes/no:"; \
  5. last if $a =~ /^yes$/;}
  6. # or
  7. while (1){ \
  8. my $a=<STDIN>; \
  9. print "quit?yes/no:"; \
  10. last if $a =~ /^yes$/;}
  11. #2.2. read user input in while statement with assignmnet in conditional
  12. while (<>){ \
  13. print "quit?yes/no:"; \
  14. last if /^yes$/;}
  15. ------------output----------------
  1. DB<2> while (true){ \
  2. cont: my $a=<>; \
  3. cont: print "quit?yes/no:"; \
  4. cont: last if $a =~ /^yes$/;}
  5. quit?yes/no:
  6. quit?yes/no:no
  7. quit?yes/no:yes
  8. quit?yes/no:
  9. DB<3>
  1. #1.1. read user input in while statement example
  2. while True:
  3. name=raw_input('quit?yes/no:')
  4. if name == 'yes': break
  5. # or
  6. while 1:
  7. name=raw_input('quit?yes/no:')
  8. if name == 'yes': break
  9. #2.2. read user input in while statement with assignmnet in conditional
  10. # no "assignment in condition" for python
  11. ---------------output------------------------
  1. >>> while True:
  2. ... name=raw_input('quit?yes/no:')
  3. ... if name == 'yes': break
  4. ...
  5. quit?yes/no:
  6. quit?yes/no:no
  7. quit?yes/no:yes
  8. >>>

Flow control: if clause

if empty???

Tasks:
1.1. if statement example
2.2. other  alternatives for 'if statement'
		  
Bash Perl Python
  1. #1.1. if statement example
  2. d=-1;
  3. if [ $d -gt 0 ]; then
  4. echo "positive";
  5. elif (( $d < 0 )); then
  6. echo "negative";
  7. else
  8. echo "zero";
  9. fi
  10. #2.2. other alternatives for 'if statement'
  11. #(())is only for numeric comarison combined with sign <, >, ==.
  12. d=-1
  13. (( $d < 0 )) && echo "negative"
  14. #[[ ]] is not recommended, why not use (( $d > 0 ))
  15. d=-1
  16. [[ $d -lt 0 ]] && echo "negative"
  17. # test is the same as []
  18. d=-1
  19. test $d -lt 0 && echo "negative"
  20. #[]
  21. d=-1
  22. [ $d -lt 0 ] && echo "negative"
  23. -------output----------
  1. #1.1. if statement example
  2. negative
  3. #2.2. other alternatives for 'if statement'
  4. #(())is only for numeric comarison combined with sign <, >, ==.
  5. negative
  6. #[[ ]] is not recommended, why not use (( -1 > 0 ))
  7. negative
  8. # test is the same as []
  9. negative
  10. #[]
  11. negative
  1. #1.1. if statement example
  2. $d=-1;
  3. if ($d > 0) { print "positive"; }
  4. elsif ($d < 0) { print "negative"; }
  5. else { print "zero";}
  6. #2.2. other alternatives for 'if statement'
  7. $d=-1;
  8. print( ($d<0)?"negative":"" );
  9. -------output----------
  1. #1.1. if statement example
  2. negative#2.2. other alternatives for 'if statement'
  3. negative
  1. #1.1. if statement example
  2. d=-1
  3. if d > 0:
  4. print("positive")
  5. elif d < 0:
  6. print("negative")
  7. else:
  8. print("zero")
  9. #2.2. other alternatives for 'if statement'
  10. d=-1
  11. print("negative") if d < 0 else "not negative"
  12. result = "negative" if d < 0 else "not negative"
  13. print result
  14. -------output----------
  1. #1.1. if statement example
  2. negative
  3. #2.2. other alternatives for 'if statement'
  4. negative
  5. negative

Flow control: continue and break(next, last in perl)

In perl, next and last are used instead of continue and break.

In python, you can use 'else' with 'for loop', which mean there is not break happens during the 'for loop' execution 

Tasks:

		  
Bash Perl Python
  1. function func(){
  2. for i in $(seq 0 $1); do
  3. if [ $i -eq 7 ]; then echo "break!"; break; fi
  4. if [ $i -eq 2 ]; then continue; fi
  5. echo -n "$i "
  6. if [ $i -eq $1 ]; then echo "no break hit"; fi
  7. done
  8. }
  9. func 10
  10. func 5
  11. -------output----------
  1. 0 1 3 4 5 6 break!
  2. 0 1 3 4 5 no break hit
  1. sub func {
  2. foreach $i (0..$_[0]){
  3. if($i==7){ print "break!\n"; last}
  4. next if $i==2;
  5. print "$i ";
  6. print "no break hit\n" if $i==$_[0];
  7. }
  8. }
  9. func(10);
  10. func(5);
  11. -------output----------
  1. 0 1 3 4 5 6 break!
  2. 0 1 3 4 5 no break hit
  1. def func(p1):
  2. for i in xrange(p1+1):
  3. if i==7: print "break!"; break
  4. if i==2: continue
  5. print i,
  6. else:
  7. print "no break hit"
  8. func(10)
  9. func(5)
  10. -------output----------
  1. 0 1 3 4 5 6 break!
  2. 0 1 3 4 5 no break hit

Flow control: numeric comparison

In bash: '==' is for comparing strings, while 'eq' is for comparing numbers with the exception that if you use '(())', then '==' is for comparing numbers.
In perl: '==' is for comparing numbers; while 'eq' is for comparing strings.
In python: '==' is for all objects comparison, however remeber to use '==' when comparing values and 'is' when comparing identities.

Tasks:
1.1. define a function named compare_num()
2.2. call the function with two number as parameters
		  
Bash Perl Python
  1. #1.1. define a function named compare_num()
  2. compare_num() {
  3. local d1=$1
  4. local d2=$2
  5. [[ $d1 -lt $d2 ]] && echo "$d1 < $d2"
  6. [[ $d1 -eq $d2 ]] && echo "$d1 == $d2"
  7. [[ $d1 -ne $d2 ]] && echo "$d1 != $d2"
  8. }
  9. #2.2. call the function with two number as parameters
  10. compare_num 7 7
  11. compare_num 7 11
  12. -------output----------
  1. #1.1. define a function named compare_num()
  2. #2.2. call the function with two number as parameters
  3. 7 == 7
  4. 7 < 11
  5. 7 != 11
  1. #1.1. define a function named compare_num()
  2. sub compare_num {
  3. # or my ($d1, $d2) = @_;
  4. my $d1 = shift;
  5. my $d2 = shift;
  6. print "$d1 < $d2\n" if $d1 < $d2;
  7. print "$d1 == $d2\n" if $d1 == $d2;
  8. print "$d1 != $d2\n" if $d1 != $d2;
  9. print "$d1 cmp $d2 is:", $d1 <=> $d2, "\n";
  10. };
  11. #2.2. call the function with two number as parameters
  12. compare_num(7, 7);
  13. compare_num(7,11);
  14. -------output----------
  1. #1.1. define a function named compare_num()
  2. #2.2. call the function with two number as parameters
  3. # or my (, ) = 7 7;
  4. 7 == 7
  5. 7 cmp 7 is:0
  6. # or my (, ) = 7 11;
  7. 7 < 11
  8. 7 != 11
  9. 7 cmp 11 is:-1
  1. #1.1. define a function named compare_num()
  2. def compare_num(d1, d2):
  3. print "d1 type is: " + str(type(d1))
  4. if d1<d2: print('%r < %r' %(d1, d2))
  5. if d1==d2: print('%r == %r' %(d1, d2))
  6. if d1!=d2: print('%r != %r' %(d1, d2))
  7. if d1 is d2: print('%r is %r' %(d1, d2))
  8. if d1 is not d2: print('%r is not %r' %(d1, d2))
  9. #2.2. call the function with two number as parameters
  10. compare_num(7, 7)
  11. compare_num(7,11)
  12. # can also compare strings
  13. compare_num('11', '7')
  14. compare_num('hello', 'hello')
  15. compare_num('hello', 'Hello')
  16. -------output----------
  1. #1.1. define a function named compare_num()
  2. #2.2. call the function with two number as parameters
  3. d1 type is: <type 'int'>
  4. 7 == 7
  5. 7 is 7
  6. d1 type is: <type 'int'>
  7. 7 < 11
  8. 7 != 11
  9. 7 is not 11
  10. # can also compare strings
  11. d1 type is: <type 'str'>
  12. '11' < '7'
  13. '11' != '7'
  14. '11' is not '7'
  15. d1 type is: <type 'str'>
  16. 'hello' == 'hello'
  17. 'hello' is 'hello'
  18. d1 type is: <type 'str'>
  19. 'hello' != 'Hello'
  20. 'hello' is not 'Hello'

Flow control: string comparison


		  
		  

Tasks:
1.1. define a function named compare_str()
2.2. call the function with two strings as parameters
3.3. notes
		  
Bash Perl Python
  1. #1.1. define a function named compare_str()
  2. compare_str() {
  3. local s1=$1
  4. local s2=$2
  5. [[ $s1 < $s2 ]] && echo "$s1 < $s2"
  6. [[ $s1 = $s2 ]] && echo "$s1 == $s2"
  7. [[ $s1 != $s2 ]] && echo "$s1 != $s2"
  8. }
  9. #2.2. call the function with two strings as parameters
  10. compare_str 'hello' 'hello'
  11. compare_str 'hello' 'Hello'
  12. compare_str '10' '9'
  13. #3.3. notes
  14. #[] and [[]] have much in common, however [[]] is only for string comparison
  15. s="Scarborough Fair"
  16. [ s = s ] && echo "$s equals $s"
  17. [[ s = s ]] && echo "$s equals $s"
  18. -------output----------
  1. #1.1. define a function named compare_str()
  2. #2.2. call the function with two strings as parameters
  3. hello == hello
  4. hello < Hello
  5. hello != Hello
  6. 10 < 9
  7. 10 != 9
  8. #3.3. notes
  9. #[] and [[]] have much in common, however [[]] is only for string comparison
  10. Scarborough Fair equals Scarborough Fair
  11. Scarborough Fair equals Scarborough Fair
  1. #1.1. define a function named compare_str()
  2. sub compare_str {
  3. my ($s1, $s2) = @_;
  4. print "$s1 < $s2\n" if $s1 lt $s2;
  5. print "$s1 == $s2\n" if $s1 eq $s2;
  6. print "$s1 != $s2\n" if $s1 ne $s2;
  7. print "$s1 cmp $s2 is:", $s1 cmp $s2, "\n";
  8. }
  9. #2.2. call the function with two strings as parameters
  10. compare_str('hello', 'hello');
  11. compare_str('hello', 'Hello');
  12. compare_str('10', '9');
  13. compare_str(9, 10);
  14. -------output----------
  1. #1.1. define a function named compare_str()
  2. #2.2. call the function with two strings as parameters
  3. hello == hello
  4. hello cmp hello is:0
  5. hello != Hello
  6. hello cmp Hello is:1
  7. 10 < 9
  8. 10 != 9
  9. 10 cmp 9 is:-1
  10. 9 != 10
  11. 9 cmp 10 is:1
  1. #1.1. define a function named compare_str()
  2. def compare_str(s1, s2):
  3. print "s1 type is: " + str(type(s1))
  4. if s1<s2: print('%r < %r' %(s1, s2))
  5. if s1==s2: print('%r == %r' %(s1, s2))
  6. if s1!=s2: print('%r != %r' %(s1, s2))
  7. if s1 is s2: print('%r is %r' %(s1, s2))
  8. if s1 is not s2: print('%r is not %r' %(s1, s2))
  9. #2.2. call the function with two strings as parameters
  10. compare_str('hello', 'hello')
  11. compare_str('hello', 'Hello')
  12. compare_str('10', '9')
  13. # can also compare numbers
  14. compare_str(9, 10)
  15. -------output----------
  1. #1.1. define a function named compare_str()
  2. #2.2. call the function with two strings as parameters
  3. s1 type is: <type 'str'>
  4. 'hello' == 'hello'
  5. 'hello' is 'hello'
  6. s1 type is: <type 'str'>
  7. 'hello' != 'Hello'
  8. 'hello' is not 'Hello'
  9. s1 type is: <type 'str'>
  10. '10' < '9'
  11. '10' != '9'
  12. '10' is not '9'
  13. # can also compare numbers
  14. s1 type is: <type 'int'>
  15. 9 < 10
  16. 9 != 10
  17. 9 is not 10

Flow control: compound conditional


		  
		  

Tasks:
1.1. logic operator for "or"
2.2. alternative expression
		  
Bash Perl Python
  1. #1.1. logic operator for "or"
  2. d=-1;
  3. if [ $d -eq 0 ] || [ $d -lt 0 ]; then echo "$d<=0"; fi
  4. #2.2. alternative expression
  5. d=-1
  6. if [[ $d -eq 0 || $d -lt 0 ]]; then echo "$d<=0"; fi
  7. d=1
  8. if (( $d == 0 || $d < 0 )); then echo "$d<=0"; fi
  9. -------output----------
  1. #1.1. logic operator for or
  2. -1<=0
  3. #2.2. alternative expression
  4. -1<=0
  1. #1.1. logic operator for "or"
  2. $d=-1;
  3. # similar to (()) in bash
  4. if(($d==0)||($d<0)) {print "$d<=0"}
  5. -------output----------
  1. #1.1. logic operator for "or"
  2. # similar to (()) in bash
  3. -1<=0
  1. #1.1. logic operator for "or"
  2. d=-1
  3. if d==0 | d<0: print str(d)+"<=0"
  4. #2.2. alternative expression
  5. d=-1
  6. if (d==0)|(d<0): print str(d)+"<=0"
  7. if d==0 or d<0: print str(d)+"<=0"
  8. -------output----------
  1. #1.1. logic operator for "or"
  2. -1<=0
  3. #2.2. alternative expression
  4. -1<=0
  5. -1<=0