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


Topics about map

Map: Get key-value pairs from map


		  
		  

Tasks:
1.Create a map
2.Print all the item in the map
3.Print all the keys and values
		  
Bash Perl Python
  1. #1.Create a map
  2. # use associative array as map in Bash 4
  3. declare -A ages
  4. ages=([dad]=42 [mom]=40 [me]=7)
  5. #2.Print all the item in the map
  6. declare -p ages
  7. -------output----------
  1. #1.Create a map
  2. # use associative array as map in Bash 4
  3. #2.Print all the item in the map
  4. declare -A ages='([mom]="40" [dad]="42" [me]="7" )'
  1. #1.Create a map
  2. %ages1=(dad=>42,mom=>40,me=>7);
  3. # or use array to define
  4. %ages2=("dad",42,"mom",40,"me",7);
  5. # or use qw()
  6. %ages3=qw(dad 42 mom 48 me 7);
  7. #2.Print all the item in the map
  8. # use [] to get the reference to array
  9. print "@{[%ages1]}\n";
  10. # or use () to change to array
  11. print join(" ", (%ages2))."\n";
  12. # or
  13. print join(" ",%ages3)."\n";
  14. -------output----------
  1. #1.Create a map
  2. # or use array to define
  3. # or use qw()
  4. #2.Print all the item in the map
  5. # use [] to get the reference to array
  6. mom 40 me 7 dad 42
  7. # or use () to change to array
  8. dad 42 me 7 mom 40
  9. # or
  10. dad 42 me 7 mom 48
  1. #1.Create a map
  2. # use dictionary as map
  3. ages={'dad':42,'mom':48,'me':7}
  4. #2.Print all the item in the map
  5. print ages
  6. print ages.items()
  7. -------output----------
  1. #1.Create a map
  2. # use dictionary as map
  3. #2.Print all the item in the map
  4. {'dad': 42, 'me': 7, 'mom': 48}
  5. [('dad', 42), ('me', 7), ('mom', 48)]

Map: Get keys and values from map


		  
		  

Tasks:
1.Create a map
2.Print all the keys and values in the map
		  
Bash Perl Python
  1. #1.Create a map
  2. declare -A ages=([dad]=42 [mom]=40 [me]=7)
  3. #2.Print all the keys and values in the map
  4. echo ${!ages[@]}
  5. echo ${ages[@]}
  6. -------output----------
  1. #1.Create a map
  2. #2.Print all the keys and values in the map
  3. mom dad me
  4. 40 42 7
  1. #1.Create a map
  2. %ages=(dad=>42,mom=>40,me=>7);
  3. #2.Print all the keys and values in the map
  4. print join(" ", keys %ages)."\n";
  5. print join(" ", values %ages)."\n";
  6. -------output----------
  1. #1.Create a map
  2. #2.Print all the keys and values in the map
  3. mom me dad
  4. 40 7 42
  1. #1.Create a map
  2. ages={'dad':42,'mom':48,'me':7}
  3. #2.Print all the keys and values in the map
  4. print ages.keys()
  5. print ages.values()
  6. -------output----------
  1. #1.Create a map
  2. #2.Print all the keys and values in the map
  3. ['dad', 'me', 'mom']
  4. [42, 7, 48]

Map: Look up in a map


		  
		  

Tasks:
1.Create a map
2.Lookup a key in the map
		  
Bash Perl Python
  1. #1.Create a map
  2. declare -A ages=([dad]=42 [mom]=40 [me]=7)
  3. #2.Lookup a key in the map
  4. echo ${ages[me]}
  5. -------output----------
  1. #1.Create a map
  2. #2.Lookup a key in the map
  3. 7
  1. #1.Create a map
  2. %ages=(dad=>42,mom=>40,me=>7);
  3. #2.Lookup a key in the map
  4. print $ages{"me"};
  5. -------output----------
  1. #1.Create a map
  2. #2.Lookup a key in the map
  3. 7
  1. #1.Create a map
  2. ages={'dad':42,'mom':48,'me':7}
  3. #2.Lookup a key in the map
  4. print ages['me']
  5. -------output----------
  1. #1.Create a map
  2. #2.Lookup a key in the map
  3. 7

Map: Iterate over a map


		  
		  

Tasks:
1.Create a map
2.Iterate over the map
		  
Bash Perl Python
  1. #1.Create a map
  2. declare -A ages=([dad]=42 [mom]=40 [me]=7)
  3. #2.Iterate over the map
  4. for key in ${!ages[@]}; do
  5. echo "$key ${ages[$key]}"
  6. done
  7. -------output----------
  1. #1.Create a map
  2. #2.Iterate over the map
  3. mom 40
  4. dad 42
  5. me 7
  1. #1.Create a map
  2. %ages=(dad=>42,mom=>40,me=>7);
  3. #2.Iterate over the map
  4. # use foreach
  5. foreach $key (keys %ages){print "$key $ages{$key}\n"}
  6. # or use while
  7. while (($key,$value)=each(%ages)){print "$key $value\n"}
  8. -------output----------
  1. #1.Create a map
  2. #2.Iterate over the map
  3. # use foreach
  4. me 7
  5. dad 42
  6. mom 40
  7. # or use while
  8. me 7
  9. dad 42
  10. mom 40
  1. #1.Create a map
  2. ages={'dad':42,'mom':48,'me':7}
  3. #2.Iterate over the map
  4. for key in ages:
  5. print key, ages[key]
  6. # or
  7. for key,value in ages.items():
  8. print key, value
  9. -------output----------
  1. #1.Create a map
  2. #2.Iterate over the map
  3. dad 42
  4. me 7
  5. mom 48
  6. # or
  7. dad 42
  8. me 7
  9. mom 48

Map: Delete an element in a map


		  
		  

Tasks:
1.Create a map
2.Remove the element  with the key of 'me'
		  
Bash Perl Python
  1. #1.Create a map
  2. declare -A ages=([dad]=42 [mom]=40 [me]=7)
  3. #2.Remove the element with the key of 'me'
  4. unset ages[me]
  5. declare -p ages
  6. -------output----------
  1. #1.Create a map
  2. #2.Remove the element with the key of 'me'
  3. declare -A ages='([mom]="40" [dad]="42" )'
  1. #1.Create a map
  2. %ages=(dad=>42,mom=>40,me=>7);
  3. #2.Remove the element with the key of 'me'
  4. delete $ages{me};
  5. print join(' ', %ages);
  6. -------output----------
  1. #1.Create a map
  2. #2.Remove the element with the key of 'me'
  3. mom 40 dad 42
  1. #1.Create a map
  2. ages={'dad':42,'mom':48,'me':7}
  3. #2.Remove the element with the key of 'me'
  4. del ages['me']
  5. print ages.items()
  6. -------output----------
  1. #1.Create a map
  2. #2.Remove the element with the key of 'me'
  3. [('dad', 42), ('mom', 48)]

Map: Clear all elements in a map


		  
		  

Tasks:
1.Create a map
2.Clear the map
		  
Bash Perl Python
  1. #1.Create a map
  2. declare -A ages=([dad]=42 [mom]=40 [me]=7)
  3. declare -p ages
  4. #2.Clear the map
  5. ages=()
  6. [[ "${ages[@]}" ]] && declare -p ages || echo "ages not defined or empty"
  7. # or use unset
  8. declare -A ages=([dad]=42 [mom]=40 [me]=7)
  9. unset ages
  10. [[ "${ages[@]}" ]] && declare -p ages || echo "ages not defined or empty"
  11. -------output----------
  1. #1.Create a map
  2. declare -A ages='([mom]="40" [dad]="42" [me]="7" )'
  3. #2.Clear the map
  4. ages not defined or empty
  5. # or use unset
  6. ages not defined or empty
  1. #1.Create a map
  2. %ages=(dad=>42,mom=>40,me=>7);
  3. print join(' ', %ages)."\n";
  4. #2.Clear the map
  5. %ages=();
  6. # do not use defined(%ages1), which is only for scalar
  7. print %ages? %ages: "ages not defined or empty\n";
  8. # or use undef
  9. undef %ages;
  10. print %ages? %ages: "ages not defined or empty\n";
  11. -------output----------
  1. #1.Create a map
  2. mom 40 dad 42 me 7
  3. #2.Clear the map
  4. # do not use defined(%ages1), which is only for scalar
  5. ages not defined or empty
  6. # or use undef
  7. ages not defined or empty
  1. #1.Create a map
  2. ages={'dad':42,'mom':48,'me':7}
  3. print ages
  4. #2.Clear the map
  5. ages.clear()
  6. print ages if 'ages' in locals() else "ages not defined"
  7. # or use del
  8. del ages
  9. print ages if 'ages' in locals() else "ages not defined"
  10. -------output----------
  1. #1.Create a map
  2. {'dad': 42, 'me': 7, 'mom': 48}
  3. #2.Clear the map
  4. {}
  5. # or use del
  6. ages not defined