0: #!/usr/bin/perl -w
1: #
2: # Three map one-liners for dealing with lists
3:
4: # Removing duplicates
5:
6: my @list1 = ('a', 'b', 'c', 'b', 'a', 'b', 'd', 'e');
7: my @list2 = ('x', 'y', 'y', 'z');
8: my @dedupedlist = keys %{{ map { $_ => 1 } (@list1, @list2) }};
9: print "@dedupedlist\n";
10:
11: ###################################
12:
13: # Determining whether an item is in a list
14:
15: my $item = 'aaa';
16: my @list = ('aaa', 'bbb', 'ccc', 'ddd', 'eee');
17:
18: if ({ map { $_ => 1 } @list }->{$item})
19: {
20: print "Yes!\n";
21: }
22: else
23: {
24: print "No!\n";
25: }
26:
27: ###################################
28:
29: # (The finale...)
30: # 'Diff' two lists
31:
32: my @lista = ('R', 'o', 'b', 'e', 'r', 't');
33: my @listb = ('e', 'r', 't');
34: my @difflist = map { {map { $_ => 1 } @listb}->{$_} ? () : $_ } @lista;
35: print "@difflist\n";
36:
In reply to Three map one-liners for lists by darth_vider
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |