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


Topics about variables

Variables: String, array, map summary

Bash variables are untyped, they can be assigned with many types of data. 
1. When assigning values to bash variables, make sure the variables are naked. 
2. However when bash variables are referenced, a '$' in front is needed. 

Perl variables are typed in comparison, and alway need a prefix($, @ or %) no matter whether they are referenced or assigned with values. perl have three basic types:
1. Scalar type, starting with $ such as $myscalar, represents a number or a string of text.
2. Arrays type, starting with @ sign as @myarray, are ordered list of scalar.
3. Hashes type, starting with % sign as %mymap, are unordered list of key value pair.

Python variables don't use any prefix in front, no '$,', no '@' or anything else, they are alway naked. python's main built-in data types are: 
1. Numbers type: including int, long, float, complex.
2. Iterables type
3. Sequence: strings, unicode, lists, tuples.
4. Set: set, frozenset.
5. Mapping(dict)

Tasks:
1.Define three variables for string, array and map respectively.
2.Print the string variable
3.Print the array variable
4.Print the map variable
		  
Bash Perl Python
  1. #1.Define three variables for string, array and map respectively.
  2. str1="abc";
  3. arr1=("a" "b" "c");
  4. declare -A ass1=([a]=1 [b]=2 [c]=3);
  5. #2.Print the string variable
  6. echo "$str1";
  7. #3.Print the array variable
  8. echo "${arr1[0]}"
  9. echo "${arr1[@]}"
  10. #4.Print the map variable
  11. echo "${ass1[a]}"
  12. declare -p ass1
  13. for key in ${!ass1[@]}; do
  14. echo -n "$key ${ass1[$key]} "
  15. done
  16. -------output----------
  1. #1.Define three variables for string, array and map respectively.
  2. #2.Print the string variable
  3. abc
  4. #3.Print the array variable
  5. a
  6. a b c
  7. #4.Print the map variable
  8. 1
  9. declare -A ass1='([a]="1" [b]="2" [c]="3" )'
  10. a 1 b 2 c 3
  1. #1.Define three variables for string, array and map respectively.
  2. $scalar1="abc";
  3. @arr1=("a","b","c");
  4. %hash1=(a=>1,b=>2,c=>3);
  5. #2.Print the string variable
  6. print "$scalar1\n";
  7. #3.Print the array variable
  8. print "@arr1[0]\n";
  9. print "@arr1\n";
  10. #4.Print the map variable
  11. print "$hash1{a}\n";
  12. print "@{[%hash1]}\n";
  13. while (($key,$value)=each %hash1) {
  14. print "$key $value\n"
  15. }
  16. -------output----------
  1. #1.Define three variables for string, array and map respectively.
  2. #2.Print the string variable
  3. abc
  4. #3.Print the array variable
  5. a
  6. a b c
  7. #4.Print the map variable
  8. 1
  9. c 3 a 1 b 2
  10. c 3
  11. a 1
  12. b 2
  1. #1.Define three variables for string, array and map respectively.
  2. str1="abc"
  3. list1=["a","b","c"]
  4. dict1={"a":1,"b":2,"c":3}
  5. #2.Print the string variable
  6. print str1
  7. #3.Print the array variable
  8. print list1[0]
  9. print list1
  10. #4.Print the map variable
  11. print dict1['a']
  12. print dict1
  13. for key,value in dict1.items():
  14. print key, value
  15. -------output----------
  1. #1.Define three variables for string, array and map respectively.
  2. #2.Print the string variable
  3. abc
  4. #3.Print the array variable
  5. a
  6. ['a', 'b', 'c']
  7. #4.Print the map variable
  8. 1
  9. {'a': 1, 'c': 3, 'b': 2}
  10. a 1
  11. c 3
  12. b 2