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


Topics about miscellaneous

Miscellaneous: how to use ternary assignment

Ternary conditional, which may also be called inline-if with assignment, can make your code more concise and easy to maintain.

In bash, ternary conditinal only works for numeric,the syntax is (( assign_condition ? value_when_true : value_when_false )), for example, n=2; nn=$((n>1?3:5))
For string, the wordaround is to use echo: [ condition ] && echo value_when_true || echo value_when_false 

In python, the ternary operator syntax is 'value whentrue if condition else valuewhenfalse'

Tasks:
1.1. assgin value using ternary assignment
2.2. print value with ternary assignment
		  
Bash Perl Python
  1. n=3
  2. #1.1. assgin value using ternary assignment
  3. s="I have $n `[ $n -eq 1 ] && echo apple || echo apples`"; echo $s
  4. #2.2. print value with ternary assignment
  5. echo "I have $n `[ $n -eq 1 ] && echo apple || echo apples`"
  6. printf "I have %d %s\n" $n `[ $n -eq 1 ] && echo apple || echo apples`
  7. -------output----------
  1. #1.1. assgin value using ternary assignment
  2. I have 3 apples
  3. #2.2. print value with ternary assignment
  4. I have 3 apples
  5. I have 3 apples
  1. $n=3;
  2. #1.1. assgin value using ternary assignment
  3. $s = "I have $n ".(($n==1)? "apple": "apples");print "$s\n";
  4. #2.2. print value with ternary assignment
  5. print "I have $n ".(($n==1)? "apple": "apples")."\n";
  6. printf "I have %d %s.\n", $n, ($n == 1) ? "apple" : "apples";
  7. -------output----------
  1. #1.1. assgin value using ternary assignment
  2. I have 3 apples
  3. #2.2. print value with ternary assignment
  4. I have 3 apples
  5. I have 3 apples.
  1. n=3
  2. #1.1. assgin value using ternary assignment
  3. s="I have "+str(n)+(" apple" if n==1 else " apples")
  4. print s
  5. #2.2. print value with ternary assignment
  6. print "I have", n, "apple" if n==1 else "apples"
  7. print "I have %d %s" % (n, "apple" if n==1 else "apples")
  8. -------output----------
  1. #1.1. assgin value using ternary assignment
  2. I have 3 apples
  3. #2.2. print value with ternary assignment
  4. I have 3 apples
  5. I have 3 apples

Miscellaneous: how to use block comment

In vim, multiline codes can be commented out easily.
1. :set number
    :7,11s/^/#
    :7,11s/^#//
2. or
   ctr-v --> select the content to commnet -->shift+I-> # -->ESC
   ctr-v --> select the content to uncomment -->d

In python docstring """....""" or content inside triple double-quotes is used to code documentation. docstring will show up when you using help function.

Tasks:

		  
Bash Perl Python
  1. echo "--> Here-document is to be used to comment out codes."
  2. :<<'XXXX'
  3. block of codes to be commented
  4. XXXX
  5. echo "--> Note: no string \"XXXX\" should exist inside the here-doc"
  6. : '
  7. [bash ~]$declare -A ages
  8. [bash ~]$ages=([dad]=42 [mom]=40 [me]=7)
  9. [bash ~]$echo ${!ages[@]}
  10. mom dad me
  11. [bash ~]$echo ${ages[@]}
  12. 40 42 7
  13. '
  14. -------output----------
  1. --> Here-document is to be used to comment out codes.
  2. --> Note: no string "XXXX" should exist inside the here-doc
  1. print "--> The '=for x' directive says that only a processor that's formatting for the x(such as =html) output type should heed what's in the following block.\n";
  2. =for comment
  3. block of codes to be commented
  4. =cut
  5. print "--> No processor will format =comment output";
  6. -------output----------
  1. --> The '=for x' directive says that only a processor that's formatting for the x(such as =html) output type should heed what's in the following block.
  2. --> No processor will format =comment output
  1. print "--> triple quotes can be used for commenting out."
  2. '''
  3. multiline codes are commented
  4. '''
  5. print "--> but it's only suggested for temprary use"
  6. -------output----------
  1. --> triple quotes can be used for commenting out.
  2. --> but it's only suggested for temprary use