Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^2: Find the last item in a series of files

by jamroll (Beadle)
on Jun 20, 2017 at 17:13 UTC ( [id://1193161]=note: print w/replies, xml ) Need Help??


in reply to Re: Find the last item in a series of files
in thread Find the last item in a series of files

okay. i haven't tested this code...but, here's what i got...
# firstly, i'm gonna use the working directory, for laziness' sake! lo +l # secondly, i haven't thoroughtly tested this. 'sub external_files($$ +)' is tested, and does work according to my tests # i'm working in a windows 10 environment, apache24 and activestate's +perl 5.020002 (i think that version # is right) # # thridly, this script assumes all the files in the folder are named w +ith .xxx where each x is a digit 0..9 # fourth, this will do no error checking! it will work perfect, so lon +g as you adhere to the file extension convention # fifth, and finally, i have not tested this code ############################## # i copied this from a project i'm working on # yes. i use prototypes. SUE me! sub external_files($;$) { #* # lists files within a specified folder (eg: config, txt) # folders will not be included in this list - just the filenames onl +y # if no type is provided, *.* is assumed # type should be just "png" or "txt", no need to include a leading d +ot #* my ($folder, $type) = @_; # a location (eg: users), relative to web +root && a file type if ($type) { # the following is just in case the user of this # subroutine ignores instructions (mainly me lol) $type =~ s/(\*)*//g; # remove stars $type =~ s/(\.)*//; # remove dots $type =~ s/\///g; # remove forward slashes if ($type) { $type = ".$type"; } } if ($folder) { # same idea here as for $type # this one, however, may seem weird, but i've # found it better to account for all possibilities # rather than leave it up to the user of this # code to ensure correct params are given # # besides, i tend to forget to follow my own # instructions, so this saves me tons of head # scratching, see? $folder =~ s/(\/)*$//; # remove trailing /'s $folder =~ s/^(\/)*//; # remove leading /'s $folder =~ s/\/\//\//g; # convert //'s to / $folder .= "/"; # attach trailing /* } my @fixed; my $filespec = $folder . "*" . $type; my @dirs = glob($filespec); $folder =~ s/\./\\./g; $folder =~ s/\//\\\//g; foreach my $dir (@dirs) { if (-f $dir) { $dir =~ s/$folder//; push (@fixed, $dir); } } return @fixed; # an array #usage: my @fileList = external_files("D:/", "txt"); } # end of sub external_files($$); #sub get_last($) { # you could uncomment this line...and turn the foll +owing into a sub! #my ($folder) = @_; # and yes, i do this, too! again, sue me (i belie +ve wholeheartedly, and pedantically so, in the K.I.S.S concept) # my @files = external_files($folder); # i'll leave it up to you to ma +ke sure $folder is a valid location, but give it whatever you like, r +eally my @files = external_files("d:/myNumberedFiles"); # @ files should now contain all yer files stored in d:/myNumberedFile +s/ # now, you want the file with an extension that works out to being the + highest #? # easy! # first, i'm gonna rip through the list, and build a new one. # the new one will contain just the extension with no dots. # leading zeros will be removed from the extension. this should # result in a list with elements that are just numbers. # then, i'm gonna sort the bugger, and pit out the last element. my @exts = (); foreach my $file (@files) { $file =~ s/^(.)*\.(0)*//; # remove everything before and including t +he dot and any leading zeros after the dot # now, pop that into your list push @exts, $file; } # now sort the list! sort @exts; print $exts[$#exts]; #return $exts[$#exts]; #} # and you have yer answer... #you could drop the above "main" code into a sub of it's own, too, of +course. #just uncomment the #sub... line and the line after it, and the #retur +n and #} lines at the bottom

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

Replies are listed 'Best First'.
Re^3: Find the last item in a series of files
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".

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1193161]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-04-20 00:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found