in reply to Find the last item in a series of files
#!/usr/bin/perl # http://perlmonks.org/?node_id=1192943 use strict; use warnings; my %names; /(.*)\.(.*)/ and $names{$1}[$2] = $_ while <DATA>; print $names{$_}[-1] for sort keys %names; __DATA__ file.001 file.003 file.002 one.004 two.001 two.003 one.002 one.001 two.002
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Find the last item in a series of files
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] |
|
Re^2: Find the last item in a series of files
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] |
|
Re^2: Find the last item in a series of files
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] |