Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #12050 > unrolled thread

sorting readdir output?

Started byrichard <noreply@example.com>
First post2013-12-01 11:39 -0500
Last post2013-12-02 23:54 +0000
Articles 17 — 9 participants

Back to article view | Back to comp.lang.php


Contents

  sorting readdir output? richard <noreply@example.com> - 2013-12-01 11:39 -0500
    Re: sorting readdir output? Norman Peelman <npeelman@cfl.rr.com> - 2013-12-01 11:56 -0500
      Re: sorting readdir output? richard <noreply@example.com> - 2013-12-01 12:13 -0500
    Re: sorting readdir output? Luuk <luuk@invalid.lan> - 2013-12-01 18:32 +0100
      Re: sorting readdir output? Luuk <luuk@invalid.lan> - 2013-12-01 18:35 +0100
      Re: sorting readdir output? richard <noreply@example.com> - 2013-12-01 13:10 -0500
        Re: sorting readdir output? Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2013-12-01 13:19 -0500
    Re: sorting readdir output? Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2013-12-01 12:59 -0500
      Re: sorting readdir output? "James Harris" <james.harris.1@gmail.com> - 2013-12-01 18:10 +0000
        Re: sorting readdir output? richard <noreply@example.com> - 2013-12-01 13:19 -0500
    Re: sorting readdir output? Denis McMahon <denismfmcmahon@gmail.com> - 2013-12-01 23:41 +0000
    Re: sorting readdir output? Richard Yates <richard@yatesguitar.com> - 2013-12-02 10:54 -0800
      Re: sorting readdir output? Denis McMahon <denismfmcmahon@gmail.com> - 2013-12-02 20:24 +0000
        Re: sorting readdir output? Richard Yates <richard@yatesguitar.com> - 2013-12-02 13:02 -0800
          Re: sorting readdir output? Christoph Michael Becker <cmbecker69@arcor.de> - 2013-12-02 22:33 +0100
        Re: sorting readdir output? Tim Streater <timstreater@greenbee.net> - 2013-12-02 22:20 +0000
          Re: sorting readdir output? Denis McMahon <denismfmcmahon@gmail.com> - 2013-12-02 23:54 +0000

#12050 — sorting readdir output?

Fromrichard <noreply@example.com>
Date2013-12-01 11:39 -0500
Subjectsorting readdir output?
Message-ID<1g1k8whk9o5qm.19jj117exjuzm.dlg@40tude.net>
<?php

if ($handle = opendir('../audio/1960/')) {
    echo "Directory handle: $handle\n";
    echo "Entries:\n";

    /* This is the correct way to loop over the directory. */
    while (false !== ($entry = readdir($handle))) {
        echo "$entry\n<br>";
    }

    closedir($handle);
}
?>

This gives the output in an unsorted list.
How can I make it so the array is sorted?

http://us1.php.net/readdir

[toc] | [next] | [standalone]


#12051

FromNorman Peelman <npeelman@cfl.rr.com>
Date2013-12-01 11:56 -0500
Message-ID<l7fpn4$tf0$1@dont-email.me>
In reply to#12050
On 12/01/2013 11:39 AM, richard wrote:
> <?php
>
> if ($handle = opendir('../audio/1960/')) {
>      echo "Directory handle: $handle\n";
>      echo "Entries:\n";
>
>      /* This is the correct way to loop over the directory. */
>      while (false !== ($entry = readdir($handle))) {
>          echo "$entry\n<br>";
>      }
>
>      closedir($handle);
> }
> ?>
>
> This gives the output in an unsorted list.
> How can I make it so the array is sorted?
>
> http://us1.php.net/readdir
>

   I would think it's be obvious that you need to *SORT* the $entry 
*ARRAY* before you output anything.

-- 
Norman
Registered Linux user #461062
-Have you been to www.php.net yet?-

[toc] | [prev] | [next] | [standalone]


#12052

Fromrichard <noreply@example.com>
Date2013-12-01 12:13 -0500
Message-ID<11uneei45l1wm$.vc7xnauf2zlo.dlg@40tude.net>
In reply to#12051
On Sun, 01 Dec 2013 11:56:04 -0500, Norman Peelman wrote:

> On 12/01/2013 11:39 AM, richard wrote:
>> <?php
>>
>> if ($handle = opendir('../audio/1960/')) {
>>      echo "Directory handle: $handle\n";
>>      echo "Entries:\n";
>>
>>      /* This is the correct way to loop over the directory. */
>>      while (false !== ($entry = readdir($handle))) {
>>          echo "$entry\n<br>";
>>      }
>>
>>      closedir($handle);
>> }
>> ?>
>>
>> This gives the output in an unsorted list.
>> How can I make it so the array is sorted?
>>
>> http://us1.php.net/readdir
>>
> 
>    I would think it's be obvious that you need to *SORT* the $entry 
> *ARRAY* before you output anything.

http://s2.quickmeme.com/img/e2/e2f5aa6bfb9d5754ab2678075f95572c502ae22456f125bf4c7ac4aaf055af85.jpg

http://www.quickmeme.com/img/ca/ca04c469a453cde96a177e91997888d9ff3a015742726d1234651dbd7c5ae780.jpg

[toc] | [prev] | [next] | [standalone]


#12053

FromLuuk <luuk@invalid.lan>
Date2013-12-01 18:32 +0100
Message-ID<bf8sma-q64.ln1@luuk.invalid.lan>
In reply to#12050
On 01-12-2013 17:39, richard wrote:
> <?php
>
> if ($handle = opendir('../audio/1960/')) {
>      echo "Directory handle: $handle\n";
>      echo "Entries:\n";
>
>      /* This is the correct way to loop over the directory. */
>      while (false !== ($entry = readdir($handle))) {
>          echo "$entry\n<br>";
>      }
>
>      closedir($handle);
> }
> ?>
>
> This gives the output in an unsorted list.
> How can I make it so the array is sorted?


which array?
http://us2.php.net/manual/en/language.types.array.php

but if you DO have an array, dan sorting is easy:
http://us2.php.net/manual/en/function.sort.php

>
> http://us1.php.net/readdir
>

yep, it's all in the manual....

[toc] | [prev] | [next] | [standalone]


#12054

FromLuuk <luuk@invalid.lan>
Date2013-12-01 18:35 +0100
Message-ID<fk8sma-q64.ln1@luuk.invalid.lan>
In reply to#12053
On 01-12-2013 18:32, Luuk wrote:
> On 01-12-2013 17:39, richard wrote:
>> <?php
>>
>> if ($handle = opendir('../audio/1960/')) {
>>      echo "Directory handle: $handle\n";
>>      echo "Entries:\n";
>>
>>      /* This is the correct way to loop over the directory. */
>>      while (false !== ($entry = readdir($handle))) {
>>          echo "$entry\n<br>";
>>      }
>>
>>      closedir($handle);
>> }
>> ?>
>>
>> This gives the output in an unsorted list.
>> How can I make it so the array is sorted?
>
>
> which array?
> http://us2.php.net/manual/en/language.types.array.php
>
> but if you DO have an array, dan sorting is easy:
> http://us2.php.net/manual/en/function.sort.php
>
>>
>> http://us1.php.net/readdir
>>
>
> yep, it's all in the manual....


and there's even an extended manual:
http://lmgtfy.com/?q=how+to+get+a+sorted+list+of+files+in+php&l=1

[toc] | [prev] | [next] | [standalone]


#12057

Fromrichard <noreply@example.com>
Date2013-12-01 13:10 -0500
Message-ID<dfytgcltczo0$.n13vzen2ug28.dlg@40tude.net>
In reply to#12053
On Sun, 01 Dec 2013 18:32:59 +0100, Luuk wrote:

> On 01-12-2013 17:39, richard wrote:
>> <?php
>>
>> if ($handle = opendir('../audio/1960/')) {
>>      echo "Directory handle: $handle\n";
>>      echo "Entries:\n";
>>
>>      /* This is the correct way to loop over the directory. */
>>      while (false !== ($entry = readdir($handle))) {
>>          echo "$entry\n<br>";
>>      }
>>
>>      closedir($handle);
>> }
>> ?>
>>
>> This gives the output in an unsorted list.
>> How can I make it so the array is sorted?
> 
> 
> which array?
> http://us2.php.net/manual/en/language.types.array.php
> 
> but if you DO have an array, dan sorting is easy:
> http://us2.php.net/manual/en/function.sort.php
> 
>>
>> http://us1.php.net/readdir
>>
> 
> yep, it's all in the manual....

The array is created from $handle I believe.

[toc] | [prev] | [next] | [standalone]


#12058

FromLew Pitcher <lew.pitcher@digitalfreehold.ca>
Date2013-12-01 13:19 -0500
Message-ID<j5Lmu.65041$uW4.32159@fx30.iad>
In reply to#12057
On Sunday 01 December 2013 13:10, in comp.lang.php, "richard"
<noreply@example.com> wrote:

> On Sun, 01 Dec 2013 18:32:59 +0100, Luuk wrote:
> 
>> On 01-12-2013 17:39, richard wrote:
>>> <?php
>>>
>>> if ($handle = opendir('../audio/1960/')) {
>>>      echo "Directory handle: $handle\n";
>>>      echo "Entries:\n";
>>>
>>>      /* This is the correct way to loop over the directory. */
>>>      while (false !== ($entry = readdir($handle))) {
>>>          echo "$entry\n<br>";
>>>      }
>>>
>>>      closedir($handle);
>>> }
>>> ?>
>>>
>>> This gives the output in an unsorted list.
>>> How can I make it so the array is sorted?
>> 
>> 
>> which array?
>> http://us2.php.net/manual/en/language.types.array.php
>> 
>> but if you DO have an array, dan sorting is easy:
>> http://us2.php.net/manual/en/function.sort.php
>> 
>>>
>>> http://us1.php.net/readdir
>>>
>> 
>> yep, it's all in the manual....
> 
> The array is created from $handle I believe.

You believe wrong.

opendir() returns a "handle", which is a "resource", and a "resource" is "a
special variable, holding a reference to an external resource."

The handle returned by opendir(), that is used in readdir() and closedir()
is NOT an array or list of directory entries.

-- 
Lew Pitcher
"In Skills, We Trust"
PGP public key available upon request

[toc] | [prev] | [next] | [standalone]


#12055

FromLew Pitcher <lew.pitcher@digitalfreehold.ca>
Date2013-12-01 12:59 -0500
Message-ID<vOKmu.376035$jg4.217970@fx05.iad>
In reply to#12050
On Sunday 01 December 2013 11:39, in comp.lang.php, "richard"
<noreply@example.com> wrote:

> <?php
> 
> if ($handle = opendir('../audio/1960/')) {
>     echo "Directory handle: $handle\n";
>     echo "Entries:\n";
> 
>     /* This is the correct way to loop over the directory. */
>     while (false !== ($entry = readdir($handle))) {
>         echo "$entry\n<br>";
>     }
> 
>     closedir($handle);
> }
> ?>
> 
> This gives the output in an unsorted list.
> How can I make it so the array is sorted?

Sort it, of course.

As you read the directory, put each entry into the same array.
Once you've hit the end, sort the array.



-- 
Lew Pitcher
"In Skills, We Trust"
PGP public key available upon request

[toc] | [prev] | [next] | [standalone]


#12056

From"James Harris" <james.harris.1@gmail.com>
Date2013-12-01 18:10 +0000
Message-ID<l7fu22$qqd$1@dont-email.me>
In reply to#12055
"Lew Pitcher" <lew.pitcher@digitalfreehold.ca> wrote in message 
news:vOKmu.376035$jg4.217970@fx05.iad...

...

>> if ($handle = opendir('../audio/1960/')) {

...

>>     while (false !== ($entry = readdir($handle))) {
>>         echo "$entry\n<br>";
>>     }

...

>> This gives the output in an unsorted list.
>> How can I make it so the array is sorted?

...

> As you read the directory, put each entry into the same array.
> Once you've hit the end, sort the array.

How about using scandir?

  $ents = scandir($document_root . $dir);
  natcasesort($ents);

James

[toc] | [prev] | [next] | [standalone]


#12059

Fromrichard <noreply@example.com>
Date2013-12-01 13:19 -0500
Message-ID<mzvq2xgo2uez$.hexjvup11j82.dlg@40tude.net>
In reply to#12056
On Sun, 1 Dec 2013 18:10:09 -0000, James Harris wrote:

> "Lew Pitcher" <lew.pitcher@digitalfreehold.ca> wrote in message 
> news:vOKmu.376035$jg4.217970@fx05.iad...
> 
> ...
> 
>>> if ($handle = opendir('../audio/1960/')) {
> 
> ...
> 
>>>     while (false !== ($entry = readdir($handle))) {
>>>         echo "$entry\n<br>";
>>>     }
> 
> ...
> 
>>> This gives the output in an unsorted list.
>>> How can I make it so the array is sorted?
> 
> ...
> 
>> As you read the directory, put each entry into the same array.
>> Once you've hit the end, sort the array.
> 
> How about using scandir?
> 
>   $ents = scandir($document_root . $dir);
>   natcasesort($ents);
> 
> James

This one does what I want nicely.
thanks.

<?php
$dir = "/tmp";
$dh  = opendir($dir);
while (false !== ($filename = readdir($dh))) {
    $files[] = $filename;
}

sort($files);

print_r($files);

rsort($files);

print_r($files);

?>

[toc] | [prev] | [next] | [standalone]


#12060

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2013-12-01 23:41 +0000
Message-ID<l7ghf7$3ck$1@dont-email.me>
In reply to#12050
On Sun, 01 Dec 2013 11:39:20 -0500, richard wrote:

> <?php
> 
> if ($handle = opendir('../audio/1960/')) {
>     echo "Directory handle: $handle\n";
>     echo "Entries:\n";
> 
>     /* This is the correct way to loop over the directory. */
>     while (false !== ($entry = readdir($handle))) {
>         echo "$entry\n<br>";

Make sure here that you use the form of break tag that matches your 
doctype.

>     }
> 
>     closedir($handle);
> }
> ?>
> 
> This gives the output in an unsorted list.
> How can I make it so the array is sorted?
> 
> http://us1.php.net/readdir

What array? At the moment you're working with a directory handle, from 
which you're reading one filename at a time. They come in whatever order 
the underlying os code delivers them.

If you want them sorted, read them in to an array and sort it, and then 
output the array in order, but at the moment, in your code, there is no 
array.

Without testing, the following might work:

<?php

$files = array();

if ( $handle = opendir ( '../audio/1960/' ) ) while ( false !== ( $entry 
= readdir( $handle ) ) ) $files[] = $entry;

sort( $files );

foreach ( $files as $file ) echo "{$file}<br>\n";

?>

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [next] | [standalone]


#12073

FromRichard Yates <richard@yatesguitar.com>
Date2013-12-02 10:54 -0800
Message-ID<4olp991cbd375tc26o1al9l9cnpjoc3jpp@4ax.com>
In reply to#12050
On Sun, 1 Dec 2013 11:39:20 -0500, richard <noreply@example.com>
wrote:

Isn't "false !==" unnecessary?


><?php
>
>if ($handle = opendir('../audio/1960/')) {
>    echo "Directory handle: $handle\n";
>    echo "Entries:\n";
>
>    /* This is the correct way to loop over the directory. */
>    while (false !== ($entry = readdir($handle))) {
>        echo "$entry\n<br>";
>    }
>
>    closedir($handle);
>}
>?>
>
>This gives the output in an unsorted list.
>How can I make it so the array is sorted?
>
>http://us1.php.net/readdir

[toc] | [prev] | [next] | [standalone]


#12074

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2013-12-02 20:24 +0000
Message-ID<l7iqam$7ms$4@dont-email.me>
In reply to#12073
On Mon, 02 Dec 2013 10:54:49 -0800, Richard Yates wrote:

> On Sun, 1 Dec 2013 11:39:20 -0500, richard <noreply@example.com>
> wrote:
> 
> Isn't "false !==" unnecessary?
> 
>><?php
>>
>>if ($handle = opendir('../audio/1960/')) {
>>    echo "Directory handle: $handle\n";
>>    echo "Entries:\n";
>>
>>    /* This is the correct way to loop over the directory. */
>>    while (false !== ($entry = readdir($handle))) {
>>        echo "$entry\n<br>";
>>    }
>>
>>    closedir($handle);
>>}
>>?>
>>
>>This gives the output in an unsorted list.
>>How can I make it so the array is sorted?
>>
>>http://us1.php.net/readdir

1. Please post replies *BELOW* the points they address, this is usenet, 
we've been reading from down the page for years now and it's the way we'd 
like to continue doing it.

2. Your question could be easily answered by reading the relevant manual 
entry for the function concerned. Possibly much faster than by posting to 
usenet and waiting for a response. Perhaps you should try that next time?

3. Said manual entry ( http://www.php.net/manual/en/function.readdir.php 
) contains:

"Return Values

Returns the entry name on success or FALSE on failure.

Warning

This function may return Boolean FALSE, but may also return a non-Boolean 
value which evaluates to FALSE. Please read the section on Booleans for 
more information. Use the === operator for testing the return value of 
this function."

See: http://www.php.net/manual/en/language.types.boolean.php

Which includes:

"Converting to boolean

To explicitly convert a value to boolean, use the (bool) or (boolean) 
casts. However, in most cases the cast is unnecessary, since a value will 
be automatically converted if an operator, function or control structure 
requires a boolean argument.

See also Type Juggling.

When converting to boolean, the following values are considered FALSE:

    the boolean FALSE itself
    the integer 0 (zero)
    the float 0.0 (zero)
    the empty string, and the string "0"  <----------------------****
    an array with zero elements
    an object with zero member variables (PHP 4 only)
    the special type NULL (including unset variables)
    SimpleXML objects created from empty tags

Every other value is considered TRUE (including any resource)."

So yes, "false !==" is needed, because you can have a valid result which 
would evaluate to false (such as the valid file name "0").

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [next] | [standalone]


#12075

FromRichard Yates <richard@yatesguitar.com>
Date2013-12-02 13:02 -0800
Message-ID<l0tp99pbmdh2sjm8d46pbamle9ietj3dua@4ax.com>
In reply to#12074
On Mon, 2 Dec 2013 20:24:54 +0000 (UTC), Denis McMahon
<denismfmcmahon@gmail.com> wrote:

>On Mon, 02 Dec 2013 10:54:49 -0800, Richard Yates wrote:
>
>> On Sun, 1 Dec 2013 11:39:20 -0500, richard <noreply@example.com>
>> wrote:
>> 
>> Isn't "false !==" unnecessary?
>> 
>>><?php
>>>
>>>if ($handle = opendir('../audio/1960/')) {
>>>    echo "Directory handle: $handle\n";
>>>    echo "Entries:\n";
>>>
>>>    /* This is the correct way to loop over the directory. */
>>>    while (false !== ($entry = readdir($handle))) {
>>>        echo "$entry\n<br>";
>>>    }
>>>
>>>    closedir($handle);
>>>}
>>>?>
>>>
>>>This gives the output in an unsorted list.
>>>How can I make it so the array is sorted?
>>>
>>>http://us1.php.net/readdir
>
>1. Please post replies *BELOW* the points they address, this is usenet, 
>we've been reading from down the page for years now and it's the way we'd 
>like to continue doing it.
>
>2. Your question could be easily answered by reading the relevant manual 
>entry for the function concerned. Possibly much faster than by posting to 
>usenet and waiting for a response. Perhaps you should try that next time?
>
>3. Said manual entry ( http://www.php.net/manual/en/function.readdir.php 
>) contains:
>
>"Return Values
>
>Returns the entry name on success or FALSE on failure.
>
>Warning
>
>This function may return Boolean FALSE, but may also return a non-Boolean 
>value which evaluates to FALSE. Please read the section on Booleans for 
>more information. Use the === operator for testing the return value of 
>this function."
>
>See: http://www.php.net/manual/en/language.types.boolean.php
>
>Which includes:
>
>"Converting to boolean
>
>To explicitly convert a value to boolean, use the (bool) or (boolean) 
>casts. However, in most cases the cast is unnecessary, since a value will 
>be automatically converted if an operator, function or control structure 
>requires a boolean argument.
>
>See also Type Juggling.
>
>When converting to boolean, the following values are considered FALSE:
>
>    the boolean FALSE itself
>    the integer 0 (zero)
>    the float 0.0 (zero)
>    the empty string, and the string "0"  <----------------------****
>    an array with zero elements
>    an object with zero member variables (PHP 4 only)
>    the special type NULL (including unset variables)
>    SimpleXML objects created from empty tags
>
>Every other value is considered TRUE (including any resource)."
>
>So yes, "false !==" is needed, because you can have a valid result which 
>would evaluate to false (such as the valid file name "0").

(Sorry for the previous top post)

I understand the warning as it applies to many functions. Perhaps I
should have asked why there would ever be a valid result of a
directory read that evaluates to false. In practice, reading his own
directory, would he ever actually encounter a file named "0" or
"false"? 

[toc] | [prev] | [next] | [standalone]


#12076

FromChristoph Michael Becker <cmbecker69@arcor.de>
Date2013-12-02 22:33 +0100
Message-ID<529cfc90$0$6560$9b4e6d93@newsspool4.arcor-online.net>
In reply to#12075
Richard Yates wrote:

> I understand the warning as it applies to many functions. Perhaps I
> should have asked why there would ever be a valid result of a
> directory read that evaluates to false. In practice, reading his own
> directory, would he ever actually encounter a file named "0" or
> "false"? 

Using the explicit check for !== FALSE seems reasonable anyway.  A file
named "0" might exists, and maybe there are some strange filenames that
are returned as empty string (not sure, if that could happen, though).
The filename "false", however, == TRUE.

OTOH, implicitely checking for == FALSE, would catch an error where
opendir() returned FALSE (for instance, because the directory does not
exist).  In this case the OP's code would call readdir() with FALSE as
argument, what would cause readdir() to return NULL.

A better solution would check for the return value of opendir() in the
first place.

-- 
Christoph M. Becker

[toc] | [prev] | [next] | [standalone]


#12077

FromTim Streater <timstreater@greenbee.net>
Date2013-12-02 22:20 +0000
Message-ID<021220132220066881%timstreater@greenbee.net>
In reply to#12074
In article <l7iqam$7ms$4@dont-email.me>, Denis McMahon
<denismfmcmahon@gmail.com> wrote:

> 1. Please post replies *BELOW* the points they address, this is usenet, 
> we've been reading from down the page for years now and it's the way we'd 
> like to continue doing it.

Well you might have been, personally I prefer replies to be
interspersed so as to be next to the salient point.

-- 
Tim

"That excessive bail ought not to be required, nor excessive fines imposed,
nor cruel and unusual punishments inflicted"  --  Bill of Rights 1689

[toc] | [prev] | [next] | [standalone]


#12078

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2013-12-02 23:54 +0000
Message-ID<l7j6jp$i3g$2@dont-email.me>
In reply to#12077
On Mon, 02 Dec 2013 22:20:06 +0000, Tim Streater wrote:

> In article <l7iqam$7ms$4@dont-email.me>, Denis McMahon
> <denismfmcmahon@gmail.com> wrote:
> 
>> 1. Please post replies *BELOW* the points they address, this is usenet,
>> we've been reading from down the page for years now and it's the way
>> we'd like to continue doing it.
> 
> Well you might have been, personally I prefer replies to be interspersed
> so as to be next to the salient point.

Well, yes, but still underneath, so that the chain of comments giving 
context is reading top to bottom, not bottom to top.

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web