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


Topics about set

Set: Check membership of a value in a set

In Bash, set data structure is not support , however in bash 4, associative array can be used to simulate set

In Perl, there is no native data type for set too, we can use hash with dummy value or some cpan modules such as Set::Scalar instead.

In Python, set is supported well. 

Tasks:
1.Create a set from elements with duplicates
2.Print the set
3.Check the membership of '0'
		  
Bash Perl Python
  1. #1.Create a set from elements with duplicates
  2. arr=(0 2 4 6 8 2 4 6)
  3. declare -A set1
  4. for i in "${arr[@]}"; do set1[$i]=1; done
  5. #2.Print the set
  6. # use 'declare -p' to print
  7. declare -p set1
  8. # or use for loop
  9. for i in "${!set1[@]}"; do echo -n "$i "; done; echo
  10. #3.Check the membership of '0'
  11. [ -n "${set1[0]}" ] && echo "found 0 in set"
  12. -------output----------
  1. #1.Create a set from elements with duplicates
  2. #2.Print the set
  3. # use 'declare -p' to print
  4. declare -A set1='([0]="1" [2]="1" [4]="1" [6]="1" [8]="1" )'
  5. # or use for loop
  6. 0 2 4 6 8
  7. #3.Check the membership of '0'
  8. found 0 in set
  1. use feature ":5.10";
  2. #1.Create a set from elements with duplicates
  3. @lst=(0,2,4,6,8,2,4,6);
  4. %set1 = map { $_ => 1 } @lst;
  5. #2.Print the set
  6. say keys %set1;
  7. #3.Check the membership of '0'
  8. $set1{0} and say "found 0 in set";
  9. -------output----------
  1. #1.Create a set from elements with duplicates
  2. #2.Print the set
  3. 60284
  4. #3.Check the membership of '0'
  5. found 0 in set
  1. #1.Create a set from elements with duplicates
  2. set1 = set([0,2,4,6,8,2,6])
  3. #2.Print the set
  4. print set1
  5. # or use curly braces to define
  6. set2 = {0,2,4,6,8,2,6}
  7. print set2
  8. #3.Check the membership of '0'
  9. if 0 in set1: print "found 0 in set"
  10. -------output----------
  1. #1.Create a set from elements with duplicates
  2. #2.Print the set
  3. set([0, 8, 2, 4, 6])
  4. # or use curly braces to define
  5. set([8, 0, 2, 4, 6])
  6. #3.Check the membership of '0'
  7. found 0 in set

Set: Add or remove a member in set


		  
		  

Tasks:
1.Create a set
2.Add a new element '10'
3.delete the new element '10'
		  
Bash Perl Python
  1. #1.Create a set
  2. declare -A set1='([0]="1" [2]="1" [4]="1" [6]="1" [8]="1" )'
  3. #2.Add a new element '10'
  4. set1[10]="1"
  5. declare -p set1
  6. #3.delete the new element '10'
  7. unset set1[10]
  8. declare -p set1
  9. -------output----------
  1. #1.Create a set
  2. #2.Add a new element '10'
  3. declare -A set1='([10]="1" [0]="1" [2]="1" [4]="1" [6]="1" [8]="1" )'
  4. #3.delete the new element '10'
  5. declare -A set1='([0]="1" [2]="1" [4]="1" [6]="1" [8]="1" )'
  1. #1.Create a set
  2. %set1 = map { $_ => 1 }(0,2,4,6,8);
  3. #2.Add a new element '10'
  4. $set1{10}=1;
  5. print "$_ " foreach(keys %set1);
  6. #3.delete the new element '10'
  7. delete $set1{10};
  8. print "$_ " foreach(keys %set1);
  9. -------output----------
  1. #1.Create a set
  2. #2.Add a new element '10'
  3. 8 10 6 4 2 0 #3.delete the new element '10'
  4. 8 6 4 2 0
  1. #1.Create a set
  2. set1 = set([0,2,4,6,8])
  3. #2.Add a new element '10'
  4. set1.add(10)
  5. print set1
  6. #3.delete the new element '10'
  7. set1.remove(10)
  8. print set1
  9. # or use discard(), which will not throw error if membership not found
  10. set1.discard(9)
  11. set1.discard(8)
  12. print set1
  13. -------output----------
  1. #1.Create a set
  2. #2.Add a new element '10'
  3. set([0, 2, 4, 6, 8, 10])
  4. #3.delete the new element '10'
  5. set([0, 2, 4, 6, 8])
  6. # or use discard(), which will not throw error if membership not found
  7. set([0, 2, 4, 6])

Set: Copy a set

In Bash4
Copying associative arrays is not directly possible

Tasks:
1.Create a set named set1
2.Copy set1 to set2
3.Empty set1 then print it
4.print set2
		  
Bash Perl Python
  1. #1.Create a set named set1
  2. declare -A set1='([0]="1" [2]="1" [4]="1" [6]="1" [8]="1" )'
  3. #2.Copy set1 to set2
  4. tmpstr=$(declare -p set1)
  5. eval "declare -A set2="${tmpstr#*=}
  6. declare -p set2
  7. #3.Empty set1 then print it
  8. unset set1
  9. declare -p set1 2>/dev/null
  10. #4.print set2
  11. declare -p set2
  12. -------output----------
  1. #1.Create a set named set1
  2. #2.Copy set1 to set2
  3. declare -A set2='([0]="1" [2]="1" [4]="1" [6]="1" [8]="1" )'
  4. #3.Empty set1 then print it
  5. #4.print set2
  6. declare -A set2='([0]="1" [2]="1" [4]="1" [6]="1" [8]="1" )'
  1. #1.Create a set named set1
  2. %set1 = map { $_ => 1 }(0,2,4,6,8);
  3. #2.Copy set1 to set2
  4. %set2 = %set1;
  5. #3.Empty set1 then print it
  6. map { delete $set1{$_} } keys %set1;
  7. print "$_ " foreach(keys %set1);
  8. #4.print set2
  9. print "$_ " foreach(keys %set2);
  10. -------output----------
  1. #1.Create a set named set1
  2. #2.Copy set1 to set2
  3. #3.Empty set1 then print it
  4. #4.print set2
  5. 6 0 2 8 4
  1. #1.Create a set named set1
  2. set1 = set([0,2,4,6,8])
  3. #2.Copy set1 to set2
  4. # set2 is only a pointer to set1
  5. set2 = set1
  6. # do a shallow copy to set3
  7. set3 = set1.copy()
  8. #3.Empty set1 then print it
  9. set1.clear()
  10. print set1
  11. #4.print set2
  12. print set2
  13. # print set3
  14. print set3
  15. -------output----------
  1. #1.Create a set named set1
  2. #2.Copy set1 to set2
  3. # set2 is only a pointer to set1
  4. # do a shallow copy to set3
  5. #3.Empty set1 then print it
  6. set([])
  7. #4.print set2
  8. set([])
  9. # print set3
  10. set([0, 8, 2, 4, 6])

Set: Get intersection of two sets


		  
		  

Tasks:
1.Create two sets
2.Get intersection of the two sets
		  
Bash Perl Python
  1. #1.Create two sets
  2. declare -A set1='([0]="1" [2]="1" [4]="1" [6]="1" [8]="1" )'
  3. declare -A set2='([0]="1" [3]="1" [6]="1" [9]="1" )'
  4. #2.Get intersection of the two sets
  5. oldIFS=$IFS IFS=$'\n\t'
  6. arri=($(comm -12 <(echo "${!set1[*]}"|sort) <(echo "${!set2[*]}"|sort)))
  7. IFS=$oldIFS
  8. declare -A seti
  9. for i in "${arri[@]}"; do seti[$i]=1; done
  10. declare -p seti
  11. -------output----------
  1. #1.Create two sets
  2. #2.Get intersection of the two sets
  3. declare -A seti='([0]="1" [6]="1" )'
  1. #1.Create two sets
  2. %set1 = map { $_ => 1 }(0,2,4,6,8);
  3. %set2 = map { $_ => 1 }(0,3,6,9);
  4. #2.Get intersection of the two sets
  5. # for each $_ in %set1, lookup it in $set2, if found,it's eligable
  6. %seti = map { $_ => 1 } grep { $set2{$_} } keys %set1;
  7. print "$_ " foreach(keys %seti);
  8. -------output----------
  1. #1.Create two sets
  2. #2.Get intersection of the two sets
  3. # for each in %set1, lookup it in , if found,it's eligable
  4. 6 0
  1. #1.Create two sets
  2. set1 = set([0,2,4,6,8])
  3. set2 = set((0,3,6,9))
  4. #2.Get intersection of the two sets
  5. print set1 & set2
  6. # or use intersection() function
  7. print set1.intersection(set2)
  8. -------output----------
  1. #1.Create two sets
  2. #2.Get intersection of the two sets
  3. set([0, 6])
  4. # or use intersection() function
  5. set([0, 6])

Set: Get difference of two sets


		  
		  

Tasks:
1.Create two sets
2.Get difference of the two sets
		  
Bash Perl Python
  1. #1.Create two sets
  2. declare -A set1='([0]="1" [2]="1" [4]="1" [6]="1" [8]="1" )'
  3. declare -A set2='([0]="1" [3]="1" [6]="1" [9]="1" )'
  4. #2.Get difference of the two sets
  5. oldIFS=$IFS IFS=$'\n\t'
  6. arrd=($(comm -23 <(echo "${!set1[*]}"|sort) <(echo "${!set2[*]}"|sort)))
  7. IFS=$oldIFS
  8. declare -A setd
  9. for i in "${arrd[@]}"; do setd[$i]=1; done
  10. declare -p setd
  11. -------output----------
  1. #1.Create two sets
  2. #2.Get difference of the two sets
  3. declare -A setd='([2]="1" [4]="1" [8]="1" )'
  1. #1.Create two sets
  2. %set1 = map { $_ => 1 }(0,2,4,6,8);
  3. %set2 = map { $_ => 1 }(0,3,6,9);
  4. #2.Get difference of the two sets
  5. # for each $_ in %set1, lookup it in $set2, if not found,it's eligable
  6. %setd = map { $_ => 1 } grep { not $set2{$_} } keys %set1;
  7. print "$_ " foreach(keys %setd);
  8. -------output----------
  1. #1.Create two sets
  2. #2.Get difference of the two sets
  3. # for each in %set1, lookup it in , if not found,it's eligable
  4. 2 8 4
  1. #1.Create two sets
  2. set1 = set([0,2,4,6,8])
  3. set2 = set((0,3,6,9))
  4. #2.Get difference of the two sets
  5. print set1 - set2
  6. # or use difference() function
  7. print set1.difference(set2)
  8. -------output----------
  1. #1.Create two sets
  2. #2.Get difference of the two sets
  3. set([8, 2, 4])
  4. # or use difference() function
  5. set([8, 2, 4])

Set: Get union of two sets


		  
		  

Tasks:
1.Create two sets
2.Get union of the two sets
		  
Bash Perl Python
  1. #1.Create two sets
  2. declare -A set1='([0]="1" [2]="1" [4]="1" [6]="1" [8]="1" )'
  3. declare -A set2='([0]="1" [3]="1" [6]="1" [9]="1" )'
  4. #2.Get union of the two sets
  5. oldIFS=$IFS IFS=$'\n\t'
  6. arru=($(comm <(echo "${!set1[*]}"|sort) <(echo "${!set2[*]}"|sort)))
  7. IFS=$oldIFS
  8. declare -A setu
  9. for i in "${arru[@]}"; do setu[$i]=1; done
  10. declare -p setu
  11. -------output----------
  1. #1.Create two sets
  2. #2.Get union of the two sets
  3. declare -A setu='([0]="1" [2]="1" [3]="1" [4]="1" [6]="1" [8]="1" [9]="1" )'
  1. #1.Create two sets
  2. %set1 = map { $_ => 1 }(0,2,4,6,8);
  3. %set2 = map { $_ => 1 }(0,3,6,9);
  4. #2.Get union of the two sets
  5. # merge the two array from keys of set1 and set2
  6. %setu = map { $_ => 1 } keys %set1, keys %set2;
  7. print "$_ " foreach(keys %setu);
  8. -------output----------
  1. #1.Create two sets
  2. #2.Get union of the two sets
  3. # merge the two array from keys of set1 and set2
  4. 0 2 8 6 4 3 9
  1. #1.Create two sets
  2. set1 = set([0,2,4,6,8])
  3. set2 = set((0,3,6,9))
  4. #2.Get union of the two sets
  5. print set1 | set2
  6. # or use union() function
  7. print set1.union(set2)
  8. -------output----------
  1. #1.Create two sets
  2. #2.Get union of the two sets
  3. set([0, 2, 3, 4, 6, 8, 9])
  4. # or use union() function
  5. set([0, 2, 3, 4, 6, 8, 9])