#!/usr/bin/perl -w # # Three map one-liners for dealing with lists # Removing duplicates my @list1 = ('a', 'b', 'c', 'b', 'a', 'b', 'd', 'e'); my @list2 = ('x', 'y', 'y', 'z'); my @dedupedlist = keys %{{ map { $_ => 1 } (@list1, @list2) }}; print "@dedupedlist\n"; ################################### # Determining whether an item is in a list my $item = 'aaa'; my @list = ('aaa', 'bbb', 'ccc', 'ddd', 'eee'); if ({ map { $_ => 1 } @list }->{$item}) { print "Yes!\n"; } else { print "No!\n"; } ################################### # (The finale...) # 'Diff' two lists my @lista = ('R', 'o', 'b', 'e', 'r', 't'); my @listb = ('e', 'r', 't'); my @difflist = map { {map { $_ => 1 } @listb}->{$_} ? () : $_ } @lista; print "@difflist\n";