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

Hello, I've tried to your way.. Here's the result

My code:
use strict; use warnings; my $dirs1 = </cygdrive/c/Users/attwwwe1/Documents/dude/logs/*.logs>; my $dirs2 = </cygdrive/c/Users/attwwwe1/Documents/dude/logs2/*.logs>; my @dirs = qw($dirs1 $dirs2); @ARGV = map { <./1848_SoPW/$_/*\.log> } @dirs; if (@ARGV){ while(<>){ #somework } } else{ print "none"; }
It just prints none every single time I run my script... Not sure what's wrong but it doesn't seem to work.

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

    That’s not my way!

    my $dirs1 = </cygdrive/c/Users/attwwwe1/Documents/dude/logs/*.logs>; my $dirs2 = </cygdrive/c/Users/attwwwe1/Documents/dude/logs2/*.logs>;

    If you’ve decided to glob each directory separately, you need to store the results of each glob operation in an array, not a scalar:

    my @logs1 = </cygdrive/c/Users/attwwwe1/Documents/dude/logs/*.logs>; my @logs2 = </cygdrive/c/Users/attwwwe1/Documents/dude/logs2/*.logs>;

    And then you don’t need to glob again; just put the names of all the log files into @ARGV:

    @ARGV = (@logs1, @logs2);

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re^4: Looping through multiple directories
by haukex (Archbishop) on Dec 26, 2017 at 15:32 UTC

    Instead of two separate globs, you can do it in one, as in

    </path/to/dude/{logs,logs2}/*.logs> # or </path/to/dude/logs*/*.logs> # or even </path/to/dude/*/*.logs>

    Although the usage I showed above should be ok, glob has a few caveats one should be aware of: It does not list filenames beginning with a dot by default, and it splits its argument on whitespace, which may be important in case you are interpolating variables into the pattern - you can use File::Glob ':bsd_glob'; to alleviate the latter caveat. Make sure to read all of glob and File::Glob.