in reply to Re: Looping through multiple directories
in thread Looping through multiple directories
Hello NetWallah,
Your first assignment to @ARGV is actually an I/O command (because it uses <>).
Sorry, no:
13:35 >perl -MO=Deparse 1848_SoPW.pl use File::Glob (); @ARGV = glob('/cygdrive/c/Users/abc123/Documents/dude/logs/*.log'); while (defined($_ = readline ARGV)) { (); } 1848_SoPW.pl syntax OK 13:35 >
To dotowwxo:
From the documentation for glob:
Note that glob splits its arguments on whitespace and treats each segment as separate pattern. As such, glob("*.c *.h") matches all files with a .c or .h extension.
So, so specify multiple directories, you simply include each in the expression input to glob. But if you have a lot of directories, it may be more convenient to do something like this:
my @dirs = qw( dir1 dir2 ); # add more as required @ARGV = (); push @ARGV, <./1848_SoPW/$_/*\.log> for @dirs;
or, equivalently,
my @dirs = qw( dir1 dir2 ); @ARGV = map { <./1848_SoPW/$_/*\.log> } @dirs;
Update: The problem described by NetWallah above will occur if @ARGV should happen to be empty when the while (<>) { loop is entered. So it’s safer to handle this as a special case:
use strict; use warnings; my @dirs = qw( dir1 dir2 ); @ARGV = map { <./1848_SoPW/$_/*\.log> } @dirs; if (@ARGV) { while (<>) { ... } }
Hope that helps,
| Athanasius <°(((>< contra mundum | Iustus alius egestas vitae, eros Piratica, |
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Looping through multiple directories
by dotowwxo (Acolyte) on Dec 26, 2017 at 06:50 UTC | |
by Athanasius (Archbishop) on Dec 26, 2017 at 07:37 UTC | |
by karlgoethebier (Abbot) on Dec 26, 2017 at 09:43 UTC | |
|
Re^3: Looping through multiple directories
by dotowwxo (Acolyte) on Dec 26, 2017 at 09:39 UTC | |
by Athanasius (Archbishop) on Dec 26, 2017 at 10:15 UTC | |
by haukex (Archbishop) on Dec 26, 2017 at 15:32 UTC |