in reply to Map and Grep
my @files_and_sizes = map { [ $_, -s $_ ] } @filenames;
Here, we have an array of filenames. We use map to iterate over each element, creating an anonymous array consisting of the filename and the size of the file -- for each filename. Those end up in the array on the left hand side. (Read map statements left to right.)
There's nothing that says that the map block has to return anything related to the source list:
my @useless = map { 1 } qw( this is a silly test ); print "@useless\n";
grep applies an expression to each element in the list, returning a list of those elements for which the expression evaluated to be true.
We create two sets, then create a hash with keys from @set1. Our grep statement checks to see if each element returns a true from the block -- that is, it checks to see if each element of @set2 is a valid key in the hash. It returns a list of those keys (only 'mangoes').my @set1 = qw (apples oranges pears mangoes); my @set2 = qw (bananas mangoes papayas coconuts); my %first_set; @first_set{@set1} = (); my @union = grep { exists $first_set{$_} } @set2;
|
|---|