readdir and regex

Discussion in 'Perl Misc' started by poncenby, Jan 24, 2007.

  1. poncenby

    poncenby Guest

    my perl program accepts arguments which will match a directory under
    $path. however this argument may not be specified so all directories
    shall be processed under $path.
    this is the way i'm doing it:

    my $idopt = $ARGV[0];
    my $path = "/home/user/";
    opendir (ROOT, "$path");

    if ($idopt) { $idre = qr/$idopt/o; } elsif ( !$idopt) { $idre =
    "!/^\.\.?$/"; }

    my @dirid = grep { $idre }, readdir (ROOT);

    however the dirid array is populated with every directory under $path,
    regardless of what I specify in ARGV[0].

    can anyone help?

    thanks
     
    poncenby, Jan 24, 2007
    #1
    1. Advertising

  2. poncenby

    Mirco Wahab Guest

    poncenby wrote:
    > my perl program accepts arguments which will match a directory under
    > $path. however this argument may not be specified so all directories
    > shall be processed under $path.
    > this is the way i'm doing it:
    >
    > my $idopt = $ARGV[0];
    > my $path = "/home/user/";
    > opendir (ROOT, "$path");
    >
    > if ($idopt) { $idre = qr/$idopt/o; } elsif ( !$idopt) { $idre =
    > "!/^\.\.?$/"; }
    >
    > my @dirid = grep { $idre }, readdir (ROOT);
    >
    > however the dirid array is populated with every directory under $path,
    > regardless of what I specify in ARGV[0].


    Of course, there is a lot of things that went wrong here ;-)

    Try to modify your program step for step
    and look what happens.

    Here's my first shot at it:

    ...
    my $path = '/home/user/';
    my $idre = $ARGV[0] ? qr{$ARGV[0]} : '^(\.(?!\.))?[^\.].+';

    opendir ROOT, $path or die "open failed $!";
    my @dircont = grep /$idre/, readdir ROOT;
    closedir ROOT;

    print join "\n", @dircont;


    Regards

    Mirco
     
    Mirco Wahab, Jan 24, 2007
    #2
    1. Advertising

  3. poncenby

    -berlin.de Guest

    poncenby <> wrote in comp.lang.perl.misc:
    > my perl program accepts arguments which will match a directory under
    > $path. however this argument may not be specified so all directories
    > shall be processed under $path.
    > this is the way i'm doing it:
    >
    > my $idopt = $ARGV[0];
    > my $path = "/home/user/";
    > opendir (ROOT, "$path");
    >
    > if ($idopt) { $idre = qr/$idopt/o; } elsif ( !$idopt) { $idre =
    > "!/^\.\.?$/"; }
    >
    > my @dirid = grep { $idre }, readdir (ROOT);


    The line above is a syntax error. It can't be your real code.
    Please copy/paste code instead of re-typing it. Typos like that
    confuse the issue unnecessarily.

    Anno
     
    -berlin.de, Jan 24, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ramon
    Replies:
    5
    Views:
    10,859
    John C. Bollinger
    Jan 3, 2005
  2. electric sheep

    readdir() and S_ISDIR(stat.st_mode) question (OT ?)

    electric sheep, Mar 4, 2004, in forum: C Programming
    Replies:
    2
    Views:
    11,584
    Villy Kruse
    Mar 4, 2004
  3. John Stoffel
    Replies:
    0
    Views:
    164
    John Stoffel
    Feb 6, 2009
  4. it_says_BALLS_on_your forehead

    opendir and readdir vs glob iteration

    it_says_BALLS_on_your forehead, Dec 1, 2005, in forum: Perl Misc
    Replies:
    9
    Views:
    479
    robic0
    Dec 3, 2005
  5. Davy
    Replies:
    2
    Views:
    225
Loading...

Share This Page