fredho has asked for the wisdom of the Perl Monks concerning the following question:
I have several files which name has the same root and different suffix (ex: file.001, file.002, file.003) and I need to identify ,for each unique value of root value (file), the file which extension has the highest value (003)
Do I need to push matching file into an array before sorting elements on the extension?
Or is there an easiest way to proceed?
Thanks for the help
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Find the last item in a series of files
by tybalt89 (Monsignor) on Jun 16, 2017 at 15:27 UTC | |
| [reply] [d/l] |
by CountZero (Bishop) on Jun 16, 2017 at 16:30 UTC | |
A small improvement makes the regex a bit more specific and have it reject filenames that do not match the expected file name template.
CountZero A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James My blog: Imperial Deltronics | [reply] [d/l] |
by fredho (Novice) on Jun 16, 2017 at 16:48 UTC | |
| [reply] [d/l] |
by Marshall (Canon) on Jun 16, 2017 at 21:52 UTC | |
I direct your attention to the code by BillKSmith, tybalt89 and CountZero. This is clever in how it works. I think some further explanation may be helpful to you. This builds a HoA (Hash of Array) called %names. What is special is that the array @{$names{"name"}} is what is called a "sparse array" - not every element of the array has an assigned value. Perl allows this. If say @array only has 3 things in it, you can still assign $array[14]="Something";. A bunch of values will wind up being "undef" or undefined, but that is just fine. A numeric sort to get the "largest suffix number" is unnecessary, just using the [-1] index is enough. The sort of keys %names just puts the root names in alphabetical order. This has nothing to do with determining the highest numbered suffix. Added: look at Laurent_R's code also. I recommend that you use some adaption of the HoA code or Laurent_R's code. Both look great to me. Welcome to the group! You will get a lot of help here. In general more help is forthcoming when you demonstrate some effort on your part (which you did). | [reply] [d/l] [select] |
by jamroll (Beadle) on Jun 20, 2017 at 17:13 UTC | |
i hope this one works, and doesn't get too butchered by the rest of the monks here :D i like to think i'm pretty decent at this coding thing, so, go easy on me. i'm 100% self taught, and i have no personal group of PERL programmers in my midst - i'm alone, and i'm a one man band. sincerely, jamroll | [reply] [d/l] |
by haukex (Archbishop) on Jun 20, 2017 at 18:53 UTC | |
i haven't tested this code... Having a variety of test cases is important. I admit I haven't tested your code myself, but if you had tested it with multiple cases, you might have found that, for example, sort @exts; isn't doing what you want. Also, I can warmly recommend one of the filename manipulation modules like Path::Class, or perhaps File::Spec (a core module) - if you use the former you can even use its methods to list files in the directory (->children). A few more suggestions: Be careful with if ($folder), since that will test negative when $folder happens to be "0" (Truth and Falsehood), you probably want to use length or defined tests instead (same goes for if ($type), of course). Also, I think you might have missed a /g on your "remove dots" regex? Update 2019-08-17: Updated the link to "Truth and Falsehood". | [reply] [d/l] [select] |
Re: Find the last item in a series of files
by 1nickt (Canon) on Jun 16, 2017 at 14:47 UTC | |
Hi, what do you have so far? Can you show your code please? An array of file names seems like a good start, after you get the file suffixes. You might like Path::Tiny::iter() to find the files, and File::Basename::fileparse() for getting the filename suffix. Hope this helps!
The way forward always starts with a minimal test.
| [reply] |
Re: Find the last item in a series of files
by Laurent_R (Canon) on Jun 16, 2017 at 20:28 UTC | |
Anyway, you might avoid a sort with something like this: The if ... else statement could be reduced to a simple if statement and a Boolean operator: but I wanted to make it as easy to read as possible. | [reply] [d/l] [select] |
by Marshall (Canon) on Jun 16, 2017 at 23:54 UTC | |
I don't like your one line if..else because it is "hard to understand" and confers no execution advantage. Anyway, a nice algorithm idea that compares favorably with the HoA ideas previously posted. | [reply] |
by Laurent_R (Canon) on Jun 17, 2017 at 09:21 UTC | |
I also think that the one-line version of the if ... else statement is less easy to understand (that's why I used the other version in the complete code version), I only wanted to show it could also be made more concise. | [reply] [d/l] |
by CountZero (Bishop) on Jun 22, 2017 at 05:54 UTC | |
A simple split to two parts may not be enough depending upon the OP's filenames and how many '.' characters might be contained within those names. I think that a regex assignment would be more appropriate instead of a split. But it could be that this is all the OP needs. That's why using File::Basename seems a good idea as it provides an easy interface to extract path, files-basename and extension. CountZero A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James My blog: Imperial Deltronics | [reply] |
by Marshall (Canon) on Jun 22, 2017 at 22:13 UTC | |
by CountZero (Bishop) on Jun 24, 2017 at 08:16 UTC | |
Re: Find the last item in a series of files
by BillKSmith (Monsignor) on Jun 16, 2017 at 15:16 UTC | |
Bill
| [reply] |
A reply falls below the community's threshold of quality. You may see it by logging in. |