fork download
  1. #!/usr/bin/perl
  2.  
  3. # Idiom #277 Remove an element from a set
  4.  
  5. print "Implementation using native perl hash as a set\n\n";
  6.  
  7. use strict;
  8.  
  9. # use a hash to mimic a set
  10. my %set;
  11.  
  12. my @list = ( 'a' .. 'f' );
  13. $set{$_} = 1 for @list;
  14.  
  15. print "Contents of set:\n";
  16. print join ' ', sort keys %set;
  17.  
  18. delete $set{'c'}; # delete specific key
  19.  
  20. print "\nAfter removing elements c:\n";
  21. print join ' ', sort keys %set;
  22.  
  23. use v5.20;
  24. delete %set{'b','e'}; # delete hash slice
  25.  
  26. print "\nAfter removing elements b and e:\n";
  27. print join ' ', sort keys %set;
  28.  
Success #stdin #stdout 0.01s 5464KB
stdin
Standard input is empty
stdout
Implementation using native perl hash as a set

Contents of set:
a b c d e f
After removing elements c:
a b d e f
After removing elements b and e:
a d f