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,


In reply to Re^2: Looping through multiple directories by Athanasius
in thread Looping through multiple directories by dotowwxo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.