in reply to Re^2: Looping through multiple directories
in thread Looping through multiple directories

Hi, thank you so much for your response. May I ask what  map { <./1848_SoPW/$_/*\.log> } @dirs; does?

Replies are listed 'Best First'.
Re^4: Looping through multiple directories
by Athanasius (Archbishop) on Dec 26, 2017 at 07:37 UTC

    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,

Re^4: Looping through multiple directories
by karlgoethebier (Abbot) on Dec 26, 2017 at 09:43 UTC