Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 18th, 2007, 01:57 AM
pramodkh pramodkh is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 63 pramodkh User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 h 54 m
Reputation Power: 9
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

Reply With Quote
  #2  
Old September 18th, 2007, 02:11 AM
KevinADC's Avatar
KevinADC KevinADC is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Oct 2004
Location: Sunny Southern California
Posts: 2,687 KevinADC User rank is Lieutenant General (80000 - 90000 Reputation Level)KevinADC User rank is Lieutenant General (80000 - 90000 Reputation Level)KevinADC User rank is Lieutenant General (80000 - 90000 Reputation Level)KevinADC User rank is Lieutenant General (80000 - 90000 Reputation Level)KevinADC User rank is Lieutenant General (80000 - 90000 Reputation Level)KevinADC User rank is Lieutenant General (80000 - 90000 Reputation Level)KevinADC User rank is Lieutenant General (80000 - 90000 Reputation Level)KevinADC User rank is Lieutenant General (80000 - 90000 Reputation Level)KevinADC User rank is Lieutenant General (80000 - 90000 Reputation Level)KevinADC User rank is Lieutenant General (80000 - 90000 Reputation Level)KevinADC User rank is Lieutenant General (80000 - 90000 Reputation Level)KevinADC User rank is Lieutenant General (80000 - 90000 Reputation Level)KevinADC User rank is Lieutenant General (80000 - 90000 Reputation Level)KevinADC User rank is Lieutenant General (80000 - 90000 Reputation Level)KevinADC User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 3 Weeks 4 Days 12 h 58 m 13 sec
Reputation Power: 860
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.

Reply With Quote
  #3  
Old September 18th, 2007, 09:38 AM
vb.net vb.net is offline
Demonic Swordsman DGQB
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Apr 2003
Posts: 1,043 vb.net User rank is Second Lieutenant (5000 - 10000 Reputation Level)vb.net User rank is Second Lieutenant (5000 - 10000 Reputation Level)vb.net User rank is Second Lieutenant (5000 - 10000 Reputation Level)vb.net User rank is Second Lieutenant (5000 - 10000 Reputation Level)vb.net User rank is Second Lieutenant (5000 - 10000 Reputation Level)vb.net User rank is Second Lieutenant (5000 - 10000 Reputation Level)vb.net User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 3 Days 23 h 3 m 53 sec
Reputation Power: 82
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.

Reply With Quote
  #4  
Old September 18th, 2007, 10:55 PM
pramodkh pramodkh is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 63 pramodkh User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #5  
Old September 19th, 2007, 12:58 AM
KevinADC's Avatar
KevinADC KevinADC is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Oct 2004
Location: Sunny Southern California
Posts: 2,687 KevinADC User rank is Lieutenant General (80000 - 90000 Reputation Level)KevinADC User rank is Lieutenant General (80000 - 90000 Reputation Level)KevinADC User rank is Lieutenant General (80000 - 90000 Reputation Level)KevinADC User rank is Lieutenant General (80000 - 90000 Reputation Level)KevinADC User rank is Lieutenant General (80000 - 90000 Reputation Level)KevinADC User rank is Lieutenant General (80000 - 90000 Reputation Level)KevinADC User rank is Lieutenant General (80000 - 90000 Reputation Level)KevinADC User rank is Lieutenant General (80000 - 90000 Reputation Level)KevinADC User rank is Lieutenant General (80000 - 90000 Reputation Level)KevinADC User rank is Lieutenant General (80000 - 90000 Reputation Level)KevinADC User rank is Lieutenant General (80000 - 90000 Reputation Level)KevinADC User rank is Lieutenant General (80000 - 90000 Reputation Level)KevinADC User rank is Lieutenant General (80000 - 90000 Reputation Level)KevinADC User rank is Lieutenant General (80000 - 90000 Reputation Level)KevinADC User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 3 Weeks 4 Days 12 h 58 m 13 sec
Reputation Power: 860
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.

Reply With Quote
  #6  
Old September 19th, 2007, 01:35 AM
[H4z3] [H4z3] is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2007
Location: /usr/bin/ruby
Posts: 63 [H4z3] User rank is Sergeant Major (2000 - 5000 Reputation Level)[H4z3] User rank is Sergeant Major (2000 - 5000 Reputation Level)[H4z3] User rank is Sergeant Major (2000 - 5000 Reputation Level)[H4z3] User rank is Sergeant Major (2000 - 5000 Reputation Level)[H4z3] User rank is Sergeant Major (2000 - 5000 Reputation Level)[H4z3] User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 20 h 32 m 36 sec
Reputation Power: 27
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..

Reply With Quote
  #7  
Old September 20th, 2007, 06:59 AM
pramodkh pramodkh is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 63 pramodkh User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #8  
Old September 20th, 2007, 09:11 AM
Axweildr's Avatar
Axweildr Axweildr is offline
'fie' on me, allege-dly
Click here for more information.
 
Join Date: Mar 2003
Location: in da kitchen ...
Posts: 12,874 Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)  Folding Points: 162285 Folding Title: Super Ultimate Folder - Level 1Folding Points: 162285 Folding Title: Super Ultimate Folder - Level 1Folding Points: 162285 Folding Title: Super Ultimate Folder - Level 1Folding Points: 162285 Folding Title: Super Ultimate Folder - Level 1Folding Points: 162285 Folding Title: Super Ultimate Folder - Level 1Folding Points: 162285 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 4 Months 2 Weeks 1 Day 20 h 28 m 56 sec
Reputation Power: 6421
Send a message via Google Talk to Axweildr
Orkut
perl Code:
Original - perl Code
  1. opendir DIR, "/path/";
  2. @files=readdir DIR;
  3. @files_to_use=sort @files;
  4. foreach (@files_to_use) {
  5.    do_something($_);
  6. }


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

Reply With Quote
  #9  
Old September 24th, 2007, 01:43 AM
pramodkh pramodkh is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 63 pramodkh User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 h 54 m
Reputation Power: 9
any updates?

Reply With Quote
  #10  
Old September 24th, 2007, 02:48 AM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,385 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 1 Day 21 h 29 m 56 sec
Reputation Power: 4080
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > Opendir and readdir in PERL

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap