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


Topics about array

Array: Get the length of an array


		  
		  

Tasks:
1.Create an array
2.Print the array length
		  
Bash Perl Python
  1. #1.Create an array
  2. arr=("hatter" "duchess" "alice")
  3. #2.Print the array length
  4. echo ${#arr[*]}
  5. echo ${#arr[@]}
  6. -------output----------
  1. #1.Create an array
  2. #2.Print the array length
  3. 3
  4. 3
  1. #1.Create an array
  2. @arr=("hatter","duchess","alice");
  3. #2.Print the array length
  4. print 0+@arr."\n";
  5. print scalar(@arr)."\n";
  6. print 1+$#arr."\n";
  7. -------output----------
  1. #1.Create an array
  2. #2.Print the array length
  3. 3
  4. 3
  5. 3
  1. #1.Create an array
  2. arr=["hatter","duchess","alice"];
  3. #2.Print the array length
  4. print len(arr);
  5. -------output----------
  1. #1.Create an array
  2. #2.Print the array length
  3. 3

Array: Get the elements of an array

In perl
Double dots is called 'range operator',which is useful for writing 'foreach (1..4)' loops and for doing slice operations on arrays
for example:
@foo = @foo[$#foo-4 .. $#foo];  # slice last 5 items

Tasks:
1.Create an array
2.Print all elements
3.Print elements at index 0~2 respectively
4.Print two consecutive elements start from index 0
5.Print elements starting with index 2
6.Print the last element
		  
Bash Perl Python
  1. #1.Create an array
  2. arr=({1..4}); #or
  3. arr=(1 2 3 4)
  4. #2.Print all elements
  5. echo ${arr[@]} #or
  6. echo ${arr[*]}
  7. #3.Print elements at index 0~2 respectively
  8. echo ${arr[0]} ${arr[1]} ${arr[3]}
  9. #4.Print two consecutive elements start from index 0
  10. echo ${arr[@]:0:2}
  11. #5.Print elements starting with index 2
  12. echo ${arr[*]:2}
  13. #6.Print the last element
  14. echo ${arr[-1]} #or
  15. echo ${arr[${#arr[@]}-1]}
  16. -------output----------
  1. #1.Create an array
  2. #2.Print all elements
  3. 1 2 3 4
  4. 1 2 3 4
  5. #3.Print elements at index 0~2 respectively
  6. 1 2 4
  7. #4.Print two consecutive elements start from index 0
  8. 1 2
  9. #5.Print elements starting with index 2
  10. 3 4
  11. #6.Print the last element
  12. 4
  13. 4
  1. #1.Create an array
  2. @arr=(1 .. 4); #or
  3. @arr=(1,2,3,4);
  4. #2.Print all elements
  5. print "@arr\n"; #or
  6. print join(" ",@arr)."\n";
  7. #3.Print elements at index 0~2 respectively
  8. print "$arr[0] $arr[1] $arr[3]\n";
  9. #4.Print two consecutive elements start from index 0
  10. print "@arr[0 .. 1]\n";
  11. #5.Print elements starting with index 2
  12. print "@arr[2 .. $#arr]\n";
  13. #6.Print the last element
  14. print "@arr[-1]\n"; #or
  15. print "@arr[$#arr]";
  16. -------output----------
  1. #1.Create an array
  2. #2.Print all elements
  3. 1 2 3 4
  4. 1 2 3 4
  5. #3.Print elements at index 0~2 respectively
  6. 1 2 4
  7. #4.Print two consecutive elements start from index 0
  8. 1 2
  9. #5.Print elements starting with index 2
  10. 3 4
  11. #6.Print the last element
  12. 4
  13. 4
  1. #1.Create an array
  2. arr=range(1,5)
  3. #2.Print all elements
  4. arr #or
  5. for i in arr: print i,
  6. print
  7. #3.Print elements at index 0~2 respectively
  8. print arr[0],arr[1],arr[3]
  9. #4.Print two consecutive elements start from index 0
  10. print arr[0:2]
  11. #5.Print elements starting with index 2
  12. print arr[2:]
  13. #6.Print the last element
  14. print arr[-1]
  15. -------output----------
  1. #1.Create an array
  2. #2.Print all elements
  3. 1 2 3 4
  4. #3.Print elements at index 0~2 respectively
  5. 1 2 4
  6. #4.Print two consecutive elements start from index 0
  7. [1, 2]
  8. #5.Print elements starting with index 2
  9. [3, 4]
  10. #6.Print the last element
  11. 4

Array: Iterate over an array

Python normally uses list and tuple as array  

Tasks:
1.Create an array with three elements
2.Iterate over the array
		  
Bash Perl Python
  1. #1.Create an array with three elements
  2. arr=("hatter" "duchess" "alice")
  3. declare -p arr
  4. #2.Iterate over the array
  5. # use for loop
  6. for i in "${arr[@]}"; do echo -n "$i "; done; echo
  7. # use c style for loop
  8. for ((i=0;i<${#arr[@]};i++)); do echo -n "${arr[$i]} "; done
  9. -------output----------
  1. #1.Create an array with three elements
  2. declare -a arr='([0]="hatter" [1]="duchess" [2]="alice")'
  3. #2.Iterate over the array
  4. # use for loop
  5. hatter duchess alice
  6. # use c style for loop
  7. hatter duchess alice
  1. #1.Create an array with three elements
  2. @arr=("hatter","duchess","alice");
  3. # or
  4. @arr=qw(hatter duchess alice);
  5. #2.Iterate over the array
  6. print "$_ " foreach (@arr);
  7. -------output----------
  1. #1.Create an array with three elements
  2. # or
  3. #2.Iterate over the array
  4. hatter duchess alice
  1. #1.Create an array with three elements
  2. arr=["hatter","duchess","alice"]
  3. # tuple is immutable,and faster than list
  4. tpl=("hatter","duchess","alice")
  5. #2.Iterate over the array
  6. for i in arr: print(i),
  7. print
  8. for i in tpl: print i,
  9. -------output----------
  1. #1.Create an array with three elements
  2. # tuple is immutable,and faster than list
  3. #2.Iterate over the array
  4. hatter duchess alice
  5. hatter duchess alice

Array: Get the index of an element in an array


		  
		  

Tasks:
1.Create a array
2.Get the index of the element with value 'duchess'
		  
Bash Perl Python
  1. #1.Create a array
  2. arr=("hatter" "duchess" "alice")
  3. #2.Get the index of the element with value 'duchess'
  4. for ((i=0;i<${#arr[@]};i++));do
  5. [[ ${arr[$i]} = 'duchess' ]] && echo $i
  6. done
  7. # or use associative array
  8. declare -A ass;
  9. ass=([hatter]=0 [duchess]=1 [alice]=2);
  10. echo ${ass[duchess]}
  11. -------output----------
  1. #1.Create a array
  2. #2.Get the index of the element with value 'duchess'
  3. 1
  4. # or use associative array
  5. 1
  1. #1.Create a array
  2. @arr=("hatter","duchess","alice");
  3. #2.Get the index of the element with value 'duchess'
  4. # use index hash
  5. my %index;
  6. @index{@arr}=(0..$#arr);
  7. print $index{"duchess"}."\n";
  8. # or use List::MoreUtils
  9. use List::MoreUtils qw(first_index);
  10. print first_index {$_ eq 'duchess'} @arr;
  11. -------output----------
  1. #1.Create a array
  2. #2.Get the index of the element with value 'duchess'
  3. # use index hash
  4. 1
  5. # or use List::MoreUtils
  6. 1
  1. #1.Create a array
  2. arr=["hatter","duchess","alice"]
  3. #2.Get the index of the element with value 'duchess'
  4. print arr.index('duchess')
  5. -------output----------
  1. #1.Create a array
  2. #2.Get the index of the element with value 'duchess'
  3. 1

Array: Append elements to array


		  
		  

Tasks:
1.Create a array
2.Append a element to array
		  
Bash Perl Python
  1. #1.Create a array
  2. arr=("hatter" "duchess" "alice")
  3. #2.Append a element to array
  4. arr=(${arr[@]} rabbit)
  5. echo ${arr[@]}
  6. # or use
  7. arr+=("rabbit")
  8. echo ${arr[@]}
  9. -------output----------
  1. #1.Create a array
  2. #2.Append a element to array
  3. hatter duchess alice rabbit
  4. # or use
  5. hatter duchess alice rabbit rabbit
  1. #1.Create a array
  2. @arr=("hatter","duchess","alice");
  3. #2.Append a element to array
  4. @arr=(@arr,"rabbit");
  5. print "@arr\n";
  6. # or use push
  7. push(@arr,"rabbit");
  8. print "@arr\n";
  9. -------output----------
  1. #1.Create a array
  2. #2.Append a element to array
  3. hatter duchess alice rabbit
  4. # or use push
  5. hatter duchess alice rabbit rabbit
  1. #1.Create a array
  2. arr=["hatter","duchess","alice"]
  3. #2.Append a element to array
  4. arr=arr+['rabbit']
  5. print arr
  6. # or use unction insert
  7. arr.insert(len(arr),"rabbit")
  8. print arr
  9. -------output----------
  1. #1.Create a array
  2. #2.Append a element to array
  3. ['hatter', 'duchess', 'alice', 'rabbit']
  4. # or use unction insert
  5. ['hatter', 'duchess', 'alice', 'rabbit', 'rabbit']

Array: Insert an element into array


		  
		  

Tasks:
1.Create a array
2.Insert a element to array
		  
Bash Perl Python
  1. pos=1
  2. #1.Create a array
  3. arr=("hatter" "duchess" "alice")
  4. #2.Insert a element to array
  5. arr=(${arr[@]:0:$pos} 'rabbit' ${arr[@]:$pos})
  6. echo ${arr[@]}
  7. -------output----------
  1. #1.Create a array
  2. #2.Insert a element to array
  3. hatter rabbit duchess alice
  1. $pos=1;
  2. #1.Create a array
  3. @arr=("hatter","duchess","alice");
  4. #2.Insert a element to array
  5. splice @arr,$pos,0,"rabbit";
  6. print "@arr\n";
  7. -------output----------
  1. #1.Create a array
  2. #2.Insert a element to array
  3. hatter rabbit duchess alice
  1. pos=1
  2. #1.Create a array
  3. arr=["hatter","duchess","alice"]
  4. #2.Insert a element to array
  5. arr.insert(pos,"rabbit")
  6. print arr
  7. -------output----------
  1. #1.Create a array
  2. #2.Insert a element to array
  3. ['hatter', 'rabbit', 'duchess', 'alice']

Array: Remove elements by index


		  
		  

Tasks:
1.Create an array
2.Remove the 2nd element
3.Alternative ways to remove an element
		  
Bash Perl Python
  1. #1.Create an array
  2. arr=("hatter" "duchess" "alice")
  3. #2.Remove the 2nd element
  4. # assigning null to the 2nd element
  5. unset arr[1]
  6. echo "\${arr[@]} -->${arr[@]}"
  7. echo "\${arr[1]} -->${arr[1]}"
  8. #3.Alternative ways to remove an element
  9. arr=("hatter" "duchess" "alice")
  10. pos=1
  11. arr=(${arr[@]:0:$pos} ${arr[@]:$(($pos+1))})
  12. echo "\${arr[@]} -->${arr[@]}"
  13. echo "\${arr[1]} -->${arr[1]}"
  14. -------output----------
  1. #1.Create an array
  2. #2.Remove the 2nd element
  3. # assigning null to the 2nd element
  4. ${arr[@]} -->hatter alice
  5. ${arr[1]} -->
  6. #3.Alternative ways to remove an element
  7. ${arr[@]} -->hatter alice
  8. ${arr[1]} -->alice
  1. #1.Create an array
  2. @arr=("hatter","duchess","alice");
  3. #2.Remove the 2nd element
  4. delete $arr[1];
  5. print "\@arr -->@arr\n";
  6. print "\$arr[1] -->$arr[1]\n";
  7. #3.Alternative ways to remove an element
  8. @arr=("hatter","duchess","alice");
  9. # splice arr_name, pos, del_len, arr_insert
  10. splice @arr,1,1;
  11. print "\@arr -->@arr\n";
  12. print "\$arr[1] -->$arr[1]\n";
  13. -------output----------
  1. #1.Create an array
  2. #2.Remove the 2nd element
  3. @arr -->hatter alice
  4. $arr[1] -->
  5. #3.Alternative ways to remove an element
  6. # splice arr_name, pos, del_len, arr_insert
  7. @arr -->hatter alice
  8. $arr[1] -->alice
  1. #1.Create an array
  2. arr=["hatter","duchess","alice"]
  3. #2.Remove the 2nd element
  4. del arr[1];
  5. print "arr -->", arr
  6. print "arr[1] -->", arr[1]
  7. #3.Alternative ways to remove an element
  8. arr=["hatter","duchess","alice"]
  9. # from 1 to 2(not included) replaced
  10. arr[1:2]=[]
  11. print "arr -->", arr
  12. print "arr[1] -->", arr[1]
  13. # or use function pop
  14. arr=["hatter","duchess","alice"]
  15. arr.pop(1)
  16. print "arr -->", arr
  17. print "arr[1] -->", arr[1]
  18. -------output----------
  1. #1.Create an array
  2. #2.Remove the 2nd element
  3. arr --> ['hatter', 'alice']
  4. arr[1] --> alice
  5. #3.Alternative ways to remove an element
  6. # from 1 to 2(not included) replaced
  7. arr --> ['hatter', 'alice']
  8. arr[1] --> alice
  9. # or use function pop
  10. arr --> ['hatter', 'alice']
  11. arr[1] --> alice

Array: Remove elements by value


		  
		  

Tasks:
1.Create a array
2.Remove all elements with value of "duchess"
		  
Bash Perl Python
  1. #1.Create a array
  2. arr=("hatter" "duchess" "duchess" "alice")
  3. #2.Remove all elements with value of "duchess"
  4. arr=(${arr[@]/duchess/})
  5. echo ${arr[@]}
  6. -------output----------
  1. #1.Create a array
  2. #2.Remove all elements with value of duchess
  3. hatter alice
  1. use feature ":5.10";
  2. #1.Create a array
  3. @arr=("hatter","duchess","duchess","alice");
  4. #2.Remove all elements with value of "duchess"
  5. # use grep
  6. @arr2=grep { $_ ne "duchess" } @arr;
  7. say "@arr2";
  8. @arr3=grep ! /duchess/, @arr;
  9. say "@arr3";
  10. # or use map
  11. @arr4=map { $_ eq 'duchess'?():$_} @arr;
  12. say "@arr4";
  13. -------output----------
  1. #1.Create a array
  2. #2.Remove all elements with value of "duchess"
  3. # use grep
  4. hatter alice
  5. hatter alice
  6. # or use map
  7. hatter alice
  1. #1.Create a array
  2. arr=["hatter","duchess","duchess","alice"]
  3. #2.Remove all elements with value of "duchess"
  4. # use remove function
  5. while 'duchess' in arr: arr.remove('duchess')
  6. print arr
  7. arr=["hatter","duchess","duchess","alice"]
  8. # or use list
  9. print [i for i in arr if i != 'duchess']
  10. # or use filter and lambda
  11. print filter(lambda x: x != "duchess", arr)
  12. -------output----------
  1. #1.Create a array
  2. #2.Remove all elements with value of "duchess"
  3. # use remove function
  4. ['hatter', 'alice']
  5. # or use list
  6. ['hatter', 'alice']
  7. # or use filter and lambda
  8. ['hatter', 'alice']

Array: Replace an element


		  
		  

Tasks:
1.Create a array
2.Remove "duchess" with "hare" for all elements
		  
Bash Perl Python
  1. #1.Create a array
  2. arr=("hatter" "duchess" "duchess" "alice")
  3. #2.Remove "duchess" with "hare" for all elements
  4. echo ${arr[@]/duchess/hare}
  5. -------output----------
  1. #1.Create a array
  2. #2.Remove duchess with hare for all elements
  3. hatter hare hare alice
  1. use feature ":5.10";
  2. #1.Create a array
  3. @arr1=@arr2=@arr3=("hatter","duchess","duchess","alice");
  4. #2.Remove "duchess" with "hare" for all elements
  5. # use for loop
  6. s/duchess/hare/ for @arr1;
  7. say "@arr1";
  8. # or use map
  9. map {s/duchess/queen/g;} @arr2;
  10. say "@arr2";
  11. # or use map
  12. @arr3=map { $_ eq 'duchess' ? 'turtle':$_} @arr3;
  13. say "@arr3";
  14. -------output----------
  1. #1.Create a array
  2. #2.Remove "duchess" with "hare" for all elements
  3. # use for loop
  4. hatter hare hare alice
  5. # or use map
  6. hatter queen queen alice
  7. # or use map
  8. hatter turtle turtle alice
  1. #1.Create a array
  2. arr1=arr2=["hatter","duchess","duchess","alice"]
  3. #2.Remove "duchess" with "hare" for all elements
  4. # use for loop and enumerate
  5. for n,i in enumerate(arr1):
  6. if i=="duchess":
  7. arr1[n]="hare"
  8. print arr1
  9. # or use
  10. arr2=["hare" if i=="duchess" else i for i in arr2]
  11. print arr2
  12. -------output----------
  1. #1.Create a array
  2. #2.Remove "duchess" with "hare" for all elements
  3. # use for loop and enumerate
  4. ['hatter', 'hare', 'hare', 'alice']
  5. # or use
  6. ['hatter', 'hare', 'hare', 'alice']

Array: Sort all the elements


		  
		  

Tasks:
1.Create a array
2.Sort all the elements
		  
Bash Perl Python
  1. #1.Create a array
  2. arr=("hatter" "duchess" "alice")
  3. #2.Sort all the elements
  4. IFS=$'\r\n' arr=(`for i in "${arr[@]}"; do echo "$i"; done | sort`)
  5. echo ${arr[@]}
  6. -------output----------
  1. #1.Create a array
  2. #2.Sort all the elements
  3. alice duchess hatter
  1. #1.Create a array
  2. @arr=("hatter","duchess","alice");
  3. #2.Sort all the elements
  4. @arr=sort @arr;
  5. print "@arr";
  6. -------output----------
  1. #1.Create a array
  2. #2.Sort all the elements
  3. alice duchess hatter
  1. #1.Create a array
  2. arr=["hatter","duchess","alice"]
  3. #2.Sort all the elements
  4. arr.sort()
  5. print arr
  6. -------output----------
  1. #1.Create a array
  2. #2.Sort all the elements
  3. ['alice', 'duchess', 'hatter']

Array: Reverse all the elements


		  
		  

Tasks:
1.Create a array
2.Reverse all the elements
		  
Bash Perl Python
  1. #1.Create a array
  2. arr=("hatter" "duchess" "alice")
  3. #2.Reverse all the elements
  4. arr=(`for ((i=${#arr[@]}-1; i>=0; i--)); do echo "${arr[$i]}"; done`);
  5. echo ${arr[@]}
  6. -------output----------
  1. #1.Create a array
  2. #2.Reverse all the elements
  3. alice duchess hatter
  1. #1.Create a array
  2. @arr=("hatter","duchess","alice");
  3. #2.Reverse all the elements
  4. print join(" ",reverse(@arr));
  5. -------output----------
  1. #1.Create a array
  2. #2.Reverse all the elements
  3. alice duchess hatter
  1. #1.Create a array
  2. arr=["hatter","duchess","alice"]
  3. #2.Reverse all the elements
  4. arr.reverse()
  5. print arr
  6. # or use slice functionality
  7. print arr[::-1]
  8. -------output----------
  1. #1.Create a array
  2. #2.Reverse all the elements
  3. ['alice', 'duchess', 'hatter']
  4. # or use slice functionality
  5. ['hatter', 'duchess', 'alice']

Array: Concatenate two arrays


		  
		  

Tasks:
1.Create two arrays
2.concatenate the two arrays
		  
Bash Perl Python
  1. #1.Create two arrays
  2. arr1=("hatter" "duchess" "alice")
  3. arr2=("rabbit" "hare")
  4. #2.concatenate the two arrays
  5. arr=("${arr1[@]}" "${arr2[@]}")
  6. echo ${arr[@]}
  7. -------output----------
  1. #1.Create two arrays
  2. #2.concatenate the two arrays
  3. hatter duchess alice rabbit hare
  1. #1.Create two arrays
  2. @arr1=("hatter","duchess","alice");
  3. @arr2=qw(rabbit hare);
  4. #2.concatenate the two arrays
  5. @arr = (@arr1 , @arr2);
  6. print "@arr";
  7. -------output----------
  1. #1.Create two arrays
  2. #2.concatenate the two arrays
  3. hatter duchess alice rabbit hare
  1. #1.Create two arrays
  2. arr1=["hatter","duchess","alice"]
  3. arr2=["rabbit","hare"]
  4. #2.concatenate the two arrays
  5. arr = arr1 + arr2
  6. print arr
  7. -------output----------
  1. #1.Create two arrays
  2. #2.concatenate the two arrays
  3. ['hatter', 'duchess', 'alice', 'rabbit', 'hare']

Array: Concatenate all elements to a string with '-'


		  
		  

Tasks:
1.Create a array
2.Concatenate all elements to a string with '-'
		  
Bash Perl Python
  1. #1.Create a array
  2. arr=("hatter" "duchess" "alice")
  3. #2.Concatenate all elements to a string with '-'
  4. IFS='-'
  5. echo "${arr[*]}"
  6. unset IFS
  7. -------output----------
  1. #1.Create a array
  2. #2.Concatenate all elements to a string with '-'
  3. hatter-duchess-alice
  1. #1.Create a array
  2. @arr=("hatter","duchess","alice");
  3. #2.Concatenate all elements to a string with '-'
  4. print join('-', @arr);
  5. -------output----------
  1. #1.Create a array
  2. #2.Concatenate all elements to a string with '-'
  3. hatter-duchess-alice
  1. #1.Create a array
  2. arr=["hatter","duchess","alice"]
  3. #2.Concatenate all elements to a string with '-'
  4. print '-'.join(arr)
  5. -------output----------
  1. #1.Create a array
  2. #2.Concatenate all elements to a string with '-'
  3. hatter-duchess-alice