Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how does readdir read?

Status
Not open for further replies.

nerri

Programmer
Sep 1, 2006
21
US
I am writing a piece of code that right now I just want to read in all the subdirectories and files in this folder and just print them to file. When I print them to file, they are all there, but they are not in ascii alphebetical, they are in some other order that I can't seem to figure out.
Code:
opendir FILE, $dir || die "Cannot open $dir: !_.";
open OUTFILE "outfile.txt";

while ($lala = readdir(FILE)){
print OUTFILE "$lala\n";
}

How is the computer reading in this data?
 
I think it depends on your system. On Win32, it reads in *ASCII* alphabetical. That doesn't mean Z comes after a. That's alphabetical.

It sorts them by ASCII value. If you look up an ASCII chart (or open Character Map on Windows) you can see what order it will sort the files in.

Symbols, then numbers, then more symbols, then capital letters, then lowercase letters, then a few more symbols, and then the extended ASCII symbols (things typically used in foreign languages).

You can always just sort them yourself. readdir into an array and sort if you have some logical order you want them to be in.

-------------
Kirsle.net | Kirsle's Programs and Projects
 
I'm on a Sun machine. I know that I can just read them into an array, but I need to know the reason they are coming out in a weird order. I don't want this to signify some bug that will strike hard and fast later. It's not ordering things in ascii alphebetical, date modified, date created, date accessed, username or size. I'm totally clueless on this one.
 
the files are just being read in the order that the underlying filesystem "stores" them in. Files are never stored in any particular order that I am aware of, but when you use some type of program to see a list of the files/folders the progam puts them into a logical order for humans to understand. You need to provide your perl script with a logicl order, otherwise it will read them into your file as it finds them. There is no bug here that will bite you.

- Kevin, perl coder unexceptional!
 
Thank you Kevin, this is helpful to know. I had assumed the computer stored it in the order an ls prints to screen. Again, I appreciate your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top