Sure. This method applies to any list, not just readdir.
my $max;
while( my $file = readdir(DIR) ) {
$max = $file if $file gt $max;
}
print $max;
Note: I used "unless" and "gt" rather than "if" and "le" because everything compares greater than undef, whereas nothing compares less than undef. If you want to use the latter instead of the former, you'll have to do a priming read (i.e. $min = readdir();) before the while loop.
thor
Feel the white light, the light within
Be your own disciple, fan the sparks of will
For all of us waiting, your kingdom will come
Update: changed code to fix a logic error. Now finds max instead of min. | [reply] [d/l] [select] |
Will readdir() always return a lexically sorted list?
I didn't know that it ever did! At least in Unix, it's the unsorted "directory" order, which is more or less random after files have been deleted and added to a given directory.
| [reply] |
I don't know if readdir will always return a sorted list, but I wouldn't rely on it. (I imagine that the list is in whatever order the entries are in the structure that holds the directory information which is certainly not sorted).
Also, I don't know if your aversion to your method is because of the sorting or just reading the whole list of files into memory, but if you can live with all of the files in memory, you can use a simple for loop or List::Util's maxstr routine.
| [reply] [d/l] |
Thanks for the answers. I didn't know gt worked that way on strings but I think you mean
my $max
while ( my $file = readdir(DIR) ) {
$max = $file if $file gt $max;
}
print $max;
And yes, I wanted to avoid reading tens of thousands of lines into memory just to get to the last entry. It seemed such a waste.
As for the related question, the reason I asked is because I made a test dir, created new files in there in no particular order and readdir() returned a sorted list. Curious. Just checked with /usr/bin though and it is mostly but not entirely sorted. | [reply] [d/l] |
Regarding 'lexical', you keep using that word. I do not think it means what you think it means. | [reply] |
You are correct. s/lexical/alphanumeric/g
| [reply] |