The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Perl Programming
|
Opendir and readdir in PERL
Discuss Opendir and readdir in PERL in the Perl Programming forum on Dev Shed. Opendir and readdir in PERL Perl Programming forum discussing coding in Perl, utilizing Perl modules, and other Perl-related topics. Perl, the Practical Extraction and Reporting Language, is the choice for many for parsing textual information.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

September 18th, 2007, 01:57 AM
|
|
Contributing User
|
|
Join Date: Feb 2005
Posts: 63
Time spent in forums: 15 h 54 m
Reputation Power: 9
|
|
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
|

September 18th, 2007, 02:11 AM
|
 |
Contributing User
|
|
Join Date: Oct 2004
Location: Sunny Southern California
|
|
|
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.
|

September 18th, 2007, 09:38 AM
|
|
|
|
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.
|

September 18th, 2007, 10:55 PM
|
|
Contributing User
|
|
Join Date: Feb 2005
Posts: 63
Time spent in forums: 15 h 54 m
Reputation Power: 9
|
|
|
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?
|

September 19th, 2007, 12:58 AM
|
 |
Contributing User
|
|
Join Date: Oct 2004
Location: Sunny Southern California
|
|
|
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.
|

September 19th, 2007, 01:35 AM
|
|
Contributing User
|
|
Join Date: Jul 2007
Location: /usr/bin/ruby
|
|
|
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..
|

September 20th, 2007, 06:59 AM
|
|
Contributing User
|
|
Join Date: Feb 2005
Posts: 63
Time spent in forums: 15 h 54 m
Reputation Power: 9
|
|
|
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
|

September 20th, 2007, 09:11 AM
|
 |
'fie' on me, allege-dly
|
|
Join Date: Mar 2003
Location: in da kitchen ...
|
|
perl Code:
Original
- perl Code |
|
|
|
@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
Deta vil - the devil is in the detail, allegedly, and I use the term advisedly, allegedly ... oh, no, wait I did ...
BIT COINS ANYONE
|

September 24th, 2007, 01:43 AM
|
|
Contributing User
|
|
Join Date: Feb 2005
Posts: 63
Time spent in forums: 15 h 54 m
Reputation Power: 9
|
|
|
any updates?
|

September 24th, 2007, 02:48 AM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
|
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
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|