Hi, I'm scanning a directory, collecting logs from the Dir to a list, I called it Flow.txt. I'd like to delete the newest file from the list of logs. The logs naming goes like this: Rbck_flow_log.2017-10-01.1 Rbck_flow_log.2017-10-01.2 Rbck_flow_log.2017-10-01.3 Rbck_flow_log.2017-10-01.4 and so on... the question is how I can sort the list I collected and remove the newest file from it? The sort function does not work on the mixed alfanumeric naming. @sorted = sort { $a <=> $b } @unsorted;

The Schwartzian Transform is a common idiom, but in this case, it is much easier to use the 'sort_by' function from the CPAN module List::UtilsBy. The documentation for 'sort_by' includes an example of naturally sorting strings that contain numbers. Good Luck, Bill

Thank you all for looking in to this problem! It is really appreciated, I spent two days trying before asking for help.

I cannot install any modules on this PC it cannot access internet and it has Perl 5.8xxx

I tried to use Laurent_R code but it fails on line 3 for "use feature "say"; so I removed it. I replaced "say" with print "@sorted \n " ; and the code really sorts the list. It just prins everything on the same line and the " \n " did not work somehow.

I also wanted to remove the newest file from the list that was a major headache for me. Thank you gain for looking in to it. Tester_V

Now that I have seen the actual code for each approach, I concede that the Schwartzian Transform is probably better. Even necessary given that the OP does not have access to the module. Good Luck, Bill

I tried to use Laurent_R code but it fails on line 3 for "use feature "say"; so I removed it. I replaced "say" with print "@sorted \n " ; and the code really sorts the list. It just prins everything on the same line and the " \n " did not work somehow.

Yeah, I did not know you were stuck with such an old version of Perl. "say" was introduced with version 5.10 (which came out in 2009).

Try this:

Code

print "$_\n" for @sorted;

In Reply To

I also wanted to remove the newest file from the list that was a major headache for me.

The pop function, used on the @sorted array, will return the last item of the array (newest file) and remove it from the array.

Update: you completely changed your post after I responded to it. My answers now look weird and unrelated to your post.

Update 2: Oh, no, it is actually a new post that you posted. Sorry, our posts sort of crossed each other.

Yes, Bill, I agree that with the simple data example I posted, sort_by would probably work. But my point is that it would probably not with an example where the date within the file names would also change.

Thanks man! I really do not understand the MAP function, I see it is powerful. What does this part of the code mean (stat($_))[8]? I'm trying to "Google" it but nothing useful or understandable found so far.

the stat function returns a 13-element list with file information on its argument. The ninth element (item with subscript 8) is the last access date to the file. This may be what you need, but it is probably not, watch out that the last time the file was accessed is definitely not the same thing as the order in which the files were produced. Many events may modify the last access date after the file was produced, so that your code may not pick up the most recent file.

No, no, you got me wrong or perhaps I wasn't clear enough. I was just asking whether you had posted somewhere else, because you used stat in your code but did not seem to know what it does, so I thought that maybe someone on another forum suggested to use stat.

But I wasn't suggesting that you should post on other forums. I think that people here are willing to help you at least as much as in any other forum that I know.