in reply to Re^3: Looping through multiple directories
in thread Looping through multiple directories
Hello again dotowwxo,
map and grep are two very useful functions that Perl provides for operating on lists. Whereas grep acts as a filter, map acts as a transformer which changes each list element in some way. For example, in this one-liner:
14:20 >perl -MData::Dump -wE "dd map { $_ * 2 } 1 .. 5;" (2, 4, 6, 8, 10) 17:20 >
the integers 1, 2, 3, 4, and 5 are doubled.
By contrast, here’s an example using grep to filter out odd numbers:
17:20 >perl -MData::Dump -wE "dd grep { $_ % 2 == 0 } 1 .. 10;" (2, 4, 6, 8, 10) 17:26 >
In both map and grep, each element in the input list is represented in the block by Perl’s default variable $_. So, the expression map { <./1848_SoPW/$_/*\.log> } @dirs takes each directory name in @dirs and globs it in the expression <./1848_SoPW/$_/*\.log>. The result in this case is that map outputs a list of all the names of log files in the given directories.
Hope that helps,
| Athanasius <°(((>< contra mundum | Iustus alius egestas vitae, eros Piratica, |
|
|---|