readdir conflict problem?

Discussion in 'Perl Misc' started by Davy, Aug 24, 2006.

  1. Davy

    Davy Guest

    Hi all,

    I write a program to generate file list and directory list seperately
    from a directory.
    But I found the readdir run correctly only once. The second readdir
    always read in a null list? why?

    #-------------------------------------------------------
    use strict;
    use warnings;

    use Data::Dumper;

    my $path = "something";

    opendir(DIR, $path);

    #--- get directory list
    my @dir_list = grep{-d "$path/$_"} readdir(DIR);
    print $dir_list[1],"\n";
    print Dumper(\@dir_list);

    #--- get file list
    my @file_list = grep{-f "$path/$_"} readdir(DIR);
    print $file_list[1],"\n";
    print Dumper(\@file_list);


    closedir(DIR);
    #------------------------------------------------------------------

    Thanks!
    Davy
     
    Davy, Aug 24, 2006
    #1
    1. Advertising

  2. Davy

    David Squire Guest

    Davy wrote:
    > Hi all,
    >
    > I write a program to generate file list and directory list seperately
    > from a directory.
    > But I found the readdir run correctly only once. The second readdir
    > always read in a null list? why?


    What is wrong with the documentation?

    ----

    perldoc -f readdir:

    readdir DIRHANDLE

    Returns the next directory entry for a directory opened by "opendir". If
    used in list context, returns all the rest of the entries in the
    directory. If there are no more entries, returns an undefined value in
    scalar context or a null list in list context.

    ....

    ----

    Your first readdir (see code below) is in list context, so it returns
    everything. The second readdir thus returns a null list, as per the
    documentation.

    You need to either:

    a) store the list you get from the first readdir in an array and grep
    that (my choice)

    or

    b) close and open the directory again before the second readdir.

    Please read the documentation for the functions you are using before
    posting questions here.


    DS

    >
    > #-------------------------------------------------------
    > use strict;
    > use warnings;
    >
    > use Data::Dumper;
    >
    > my $path = "something";
    >
    > opendir(DIR, $path);
    >
    > #--- get directory list
    > my @dir_list = grep{-d "$path/$_"} readdir(DIR);
    > print $dir_list[1],"\n";
    > print Dumper(\@dir_list);
    >
    > #--- get file list
    > my @file_list = grep{-f "$path/$_"} readdir(DIR);
    > print $file_list[1],"\n";
    > print Dumper(\@file_list);
    >
    >
    > closedir(DIR);
    > #------------------------------------------------------------------
     
    David Squire, Aug 24, 2006
    #2
    1. Advertising

  3. Davy

    -berlin.de Guest

    David Squire <> wrote in comp.lang.perl.misc:
    > Davy wrote:
    > > Hi all,
    > >
    > > I write a program to generate file list and directory list seperately
    > > from a directory.
    > > But I found the readdir run correctly only once. The second readdir
    > > always read in a null list? why?

    >
    > What is wrong with the documentation?
    >
    > ----
    >
    > perldoc -f readdir:
    >
    > readdir DIRHANDLE
    >
    > Returns the next directory entry for a directory opened by "opendir". If
    > used in list context, returns all the rest of the entries in the
    > directory. If there are no more entries, returns an undefined value in
    > scalar context or a null list in list context.
    >
    > ...
    >
    > ----
    >
    > Your first readdir (see code below) is in list context, so it returns
    > everything. The second readdir thus returns a null list, as per the
    > documentation.
    >
    > You need to either:
    >
    > a) store the list you get from the first readdir in an array and grep
    > that (my choice)
    >
    > or
    >
    > b) close and open the directory again before the second readdir.


    c) rewinddir(), which exists explicitly for the purpose of reading
    a directory handle again from the beginning.

    Anno
     
    -berlin.de, Aug 24, 2006
    #3
  4. Davy

    Tony Curtis Guest

    >> On 24 Aug 2006 09:03:54 GMT,
    >> -berlin.de said:


    > David Squire <> wrote
    > in comp.lang.perl.misc:
    >> Davy wrote: > Hi all,
    >> >
    >> > I write a program to generate file list and directory

    >> list seperately > from a directory. > But I found the
    >> readdir run correctly only once. The second readdir >
    >> always read in a null list? why?
    >>


    > c) rewinddir(), which exists explicitly for the purpose
    > of reading a directory handle again from the beginning.


    d) use readdir in scalar context in a loop, then you don't
    need 2 passes, and you don't need to store all the
    directory entries in memory.

    hth
    t
     
    Tony Curtis, Aug 24, 2006
    #4
    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. Matt
    Replies:
    2
    Views:
    734
  2. Ramon
    Replies:
    5
    Views:
    10,795
    John C. Bollinger
    Jan 3, 2005
  3. electric sheep

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

    electric sheep, Mar 4, 2004, in forum: C Programming
    Replies:
    2
    Views:
    11,517
    Villy Kruse
    Mar 4, 2004
  4. charles cashion

    css conflict (or html conflict)

    charles cashion, Feb 18, 2009, in forum: HTML
    Replies:
    2
    Views:
    795
    charles cashion
    Feb 18, 2009
  5. John Stoffel
    Replies:
    0
    Views:
    153
    John Stoffel
    Feb 6, 2009
Loading...

Share This Page