#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    63
    Rep Power
    10

    Question Opendir and readdir in PERL


    Hi All,

    I have a perl script which reads the files from a directory and processes them. I am using PERL opendir and readdir commands for getting the files in the order they have created.

    But i see that its picking the files in random order.
    Is there anyway to control the order?
    Looks like this is server or perl version dependent. because I am using Unix-solaris5.8 / PERL 5.8 and it works fine in this OS.
    Same script picks the files in random fashion in Unix-solaris5.8 / PERL 5.6 version.
    Any idea what settings needs to be changed to achieve the file process order?
    my code:
    opendir( MYDIR, $Path ) or die ( "Cannot open dir ${Path}: $!" );
    while( defined( my $file = readdir(MYDIR) ) )
    {
    Process files in order;
    }
    close MYDIR;

    Thanks
    Pramod K H
  2. #2
  3. Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Oct 2004
    Location
    Sunny Southern California
    Posts
    2,687
    Rep Power
    862
    perl will read the files in whatever order they are stored on the system. It will not pick files in random order. If you want to read them in some specific order look into the "stat" function or file test operators.
  4. #3
  5. No Profile Picture
    Demonic Swordsman DGQB
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Apr 2003
    Posts
    1,043
    Rep Power
    83
    if the order is simply sorting by the file name then that can be accomplished with a sort{} operation. Otherwise, you will have to do more work.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    63
    Rep Power
    10
    Please let me know....are there any other possibilites apart from stat(i can sort using inode property) and sort?

    Files are getting picked up in random fashion for each execution.How can i control this?
  8. #5
  9. Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Oct 2004
    Location
    Sunny Southern California
    Posts
    2,687
    Rep Power
    862
    the files are not getting picked up in random order, they are getting picked up in the order the system reads them in. There are no other possibilities besides stat or file test operators and sort that I know of if you are going to use perl.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Location
    /usr/bin/ruby
    Posts
    63
    Rep Power
    29
    And well, not just Perl.. Why don't you want to use stat or sort? It would be the most ethical thing to do.. as KevinADC says; The files are stored in the order your system reads them in.. You could only control this with stat or sort. And it wouldn't even really take much..
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    63
    Rep Power
    10
    Hi All,

    finally found somedetails on google. Please go through the below link:
    http://forum.java.sun.com/thread.jspa?threadID=5183881&tstart=15

    It says:
    "sort order"? Readdir may return a specific order, but it is not "sorted".

    Can you show an example of what you're getting? How are you calling readdir? What filesystem are you using? UFS, ZFS, or something else?

    On UFS, readdir should return entries in "directory" order, based upon positioning within the directory. This is identical to what is returned from 'ls -f' within the directory. Without the -f, 'ls' itself is responsible for sorting.

    # touch z 3 a 6 p
    # ls -a
    . .. 3 6 a p z
    # ls -f
    . .. z 3 a 6 p
    # perl -e 'opendir(D,".");print join " ", readdir(D); print "\n";'
    . .. z 3 a 6 p

    ZFS uses hashing techniques rather than a linear list to maintain a directory. This improves performance in some situations, but means that the order returned is not necessarily the order that the files were created (but the order should be stable unless the directory is changed).

    /tank/test# touch z 3 a 6 p
    /tank/test# ls -f
    . .. 6 a 3 z p
    /tank/test# perl -e 'opendir(D,".");print join " ", readdir(D); print "\n";'
    . .. 6 a 3 z p


    Please let me know how can we control this server setting? cant it be done independent of this setting? Please comment on this.

    Thanks

    Pramod
  14. #8
  15. 'fie' on me, allege-dly
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2003
    Location
    in da kitchen ...
    Posts
    12,894
    Rep Power
    6444
    perl Code:
    opendir DIR, "/path/";
    @files=readdir DIR;
    closedir DIR;
    @files_to_use=sort @files;
    foreach (@files_to_use) {
       do_something($_);
    }


    File::Find might be something you could have more control over
    --Ax
    without exception, there is no rule ...
    Handmade Irish Jewellery
    Targeted Advertising Cookie Optout (TACO) extension for Firefox
    The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones


    09 F9 11 02
    9D 74 E3 5B
    D8 41 56 C5
    63 56 88 C0
    Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
    -- Jamie Zawinski
    Detavil - the devil is in the detail, allegedly, and I use the term advisedly, allegedly ... oh, no, wait I did ...
    BIT COINS ANYONE
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    63
    Rep Power
    10
    any updates?
  18. #10
  19. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,645
    Rep Power
    4248
    ZFS is a filesystem that comes with Solaris. That means that the file ordering that you see there is ZFS specific (i.e.) probably only runs on Solaris. Linux is beginning to get support for ZFS, but it isn't there yet. Also, what if the files in question are under a different filesystem. You can't convince the owner to reformat it as ZFS.

    Hence, the best solution is to read the files into an array and then call the perl function sort() to sort the files according to the desired sort order. This will work regardless of the filesystem used.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo

IMN logo majestic logo threadwatch logo seochat tools logo