Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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

In reply to Re^2: Find the last item in a series of files by jamroll
in thread Find the last item in a series of files by fredho

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (6)
As of 2024-03-28 09:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found