Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > How does Perl sort array in opendir by default?

Reply
Thread Tools

How does Perl sort array in opendir by default?

 
 
Mike
Guest
Posts: n/a
 
      01-15-2004
I'm using opendir to open a series of files in a directory, but
they're not being sorted by default the way I thought they would.

I have 3 files:
aaa.txt (765 bytes)
bbb.txt (977 bytes)
ccc.txt (10161 bytes)

I assumed that it would enter them into the array alphabetically, but
instead they went in ccc,aaa,bbb!

Is there a method to this madness? Coincidentally, it's being sorted
exactly like I want right now, but I'm not sure what will happen when
a 4th file is added.

TIA,

Mike


PS, the exact code is:

opendir FILENAMES, "$basepath";
@data= readdir FILENAMES;
closedir FILENAMES;
 
Reply With Quote
 
 
 
 
Uri Guttman
Guest
Posts: n/a
 
      01-15-2004
>>>>> "M" == Mike <(E-Mail Removed)> writes:

M> I'm using opendir to open a series of files in a directory, but
M> they're not being sorted by default the way I thought they would.

M> I assumed that it would enter them into the array alphabetically, but
M> instead they went in ccc,aaa,bbb!

where did you read in the docs that this was the case? or who taught you
that mistaken factoid?

perldoc -f readdir doesn't mention the term sort. did you check out that
doc before you made public your incorect assumption?

and there was a thread on this in the last week. try googling for it

uri

--
Uri Guttman ------ http://www.velocityreviews.com/forums/(E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
 
 
 
Sherm Pendley
Guest
Posts: n/a
 
      01-15-2004
On Wed, 14 Jan 2004 23:29:58 -0800, Mike wrote:

> I assumed that it would enter them into the array alphabetically


Why did you assume that? Nothing in the docs for readdir says anything
about them being sorted.

If you want them sorted, you have to sort them yourself; for a simple
alphabetic sort, this could be as simple as:

my @files = sort(readdir(FILENAMES));

sherm--
 
Reply With Quote
 
Mike
Guest
Posts: n/a
 
      01-15-2004
> > I assumed that it would enter them into the array alphabetically
>
> Why did you assume that? Nothing in the docs for readdir says anything
> about them being sorted.


I knew that perldoc didn't say anything, I was just making an
assumption because my server lists all of the files alphabetically by
default when you telnet in. Plus, I had no other reason to believe
otherwise. Just an assumption.

> If you want them sorted, you have to sort them yourself; for a simple
> alphabetic sort, this could be as simple as:
>
> my @files = sort(readdir(FILENAMES));


Of course, which would sort them alphabetically, or whatever you make
it do. In this case, I would prefer them to be sorted by
last-date-modified, and that's why I ask; if this is how they're
sorted by default, then there's no need for me to code it.

To Uri (who also replied), when I searched for "sort opendir array,"
the most recent post to come up was from 2002. Can you be more
specific about the similar recent post?

Mike
 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      01-15-2004
On Thu, 15 Jan 2004, Mike wrote:

> > If you want them sorted, you have to sort them yourself; for a simple
> > alphabetic sort, this could be as simple as:
> >
> > my @files = sort(readdir(FILENAMES));

>
> Of course, which would sort them alphabetically, or whatever you make
> it do. In this case, I would prefer them to be sorted by
> last-date-modified, and that's why I ask; if this is how they're
> sorted by default, then there's no need for me to code it.
>
> To Uri (who also replied), when I searched for "sort opendir array,"
> the most recent post to come up was from 2002. Can you be more
> specific about the similar recent post?
>


untested:

my @files = sort {-M $a <=> -M $b} readdir(FILENAMES);


This puts the most recently modified at the beginning of the list. For
the reverse, swap $a and $b above.

The default is whatever order they are returned by your operating system.
This could changed depending on what system your script is run on.
Therefore, trusting the default to be what you want is A Bad Idea.

Paul Lalli
 
Reply With Quote
 
Walter Roberson
Guest
Posts: n/a
 
      01-15-2004
In article <(E-Mail Removed)>,
Paul Lalli <(E-Mail Removed)> wrote:
:untested:

:my @files = sort {-M $a <=> -M $b} readdir(FILENAMES);


:This puts the most recently modified at the beginning of the list.

Schwartzian Transform unless you don't mind exponential numbers of
stat()'s.

--
I wrote a hack in microcode,
with a goto on each line,
it runs as fast as Superman,
but not quite every time! -- Don Libes et al.
 
Reply With Quote
 
Randal L. Schwartz
Guest
Posts: n/a
 
      01-15-2004
>>>>> "Mike" == Mike <(E-Mail Removed)> writes:

Mike> I knew that perldoc didn't say anything, I was just making an
Mike> assumption because my server lists all of the files alphabetically by
Mike> default when you telnet in. Plus, I had no other reason to believe
Mike> otherwise. Just an assumption.

But "ls" is doing the sorting. If you give ls the -f switch, it
presents unsorted. Also, find shows you the unsorted order as well.

readdir() returns the results unsorted because sorting is extra work,
and if you're going to do something to every element anyway, it doesn't
matter what order.

print "Just another Perl hacker,"

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<(E-Mail Removed)> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
 
Reply With Quote
 
Tassilo v. Parseval
Guest
Posts: n/a
 
      01-15-2004
Also sprach Walter Roberson:

> In article <(E-Mail Removed)>,
> Paul Lalli <(E-Mail Removed)> wrote:
>:untested:
>
>:my @files = sort {-M $a <=> -M $b} readdir(FILENAMES);
>
>
>:This puts the most recently modified at the beginning of the list.
>
> Schwartzian Transform unless you don't mind exponential numbers of
> stat()'s.


Exponential?

Tassilo
--
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus}) !JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexi ixesixeseg;y~\n~~dddd;eval
 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      01-15-2004
On Thu, 15 Jan 2004, Walter Roberson wrote:

> In article <(E-Mail Removed)>,
> Paul Lalli <(E-Mail Removed)> wrote:
> :untested:
>
> :my @files = sort {-M $a <=> -M $b} readdir(FILENAMES);
>
>
> :This puts the most recently modified at the beginning of the list.
>
> Schwartzian Transform unless you don't mind exponential numbers of
> stat()'s.
>


Quite correct. I should have done it with a transform in the first place:

(still untested):

my @files = map { $_->[0] }
sort { $a->[1] <=> $b->[1] }
map { [ $_, -M ] }
readdir FILENAMES;


Paul Lalli.
 
Reply With Quote
 
pkent
Guest
Posts: n/a
 
      01-15-2004
In article <(E-Mail Removed) >,
(E-Mail Removed) (Mike) wrote:

> I'm using opendir to open a series of files in a directory, but
> they're not being sorted by default the way I thought they would.


Sorry to break it to you but computers have a habit of sucking, and of
not doing what you want them to. Or even worse, doing exactly what you
tell them to.

You said that the computer didn't do what you thought it'd do. Even
perldoc -f readdir doesn't mention the order of entries (on my system at
least). But perl can help, using the sort() function built in to perl -
e.g.:

@filenames = sort @filenames;

for the default sorting order. If that's not good for you, you can
define your own sorting routine.

P

--
pkent 77 at yahoo dot, er... what's the last bit, oh yes, com
Remove the tea to reply
 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Perl opendir with spaces in directory names JJ Perl Misc 3 11-25-2004 08:06 AM
Simple opendir(), directory does not exist problem theo22 Perl Misc 12 01-21-2004 01:33 PM
Ado sort error-Ado Sort -Relate, Compute By, or Sort operations cannot be done on column(s) whose key length is unknown or exceeds 10 KB. Navin ASP General 1 09-09-2003 07:16 AM
opendir() Andrew Perl 2 09-03-2003 02:48 PM



Advertisments