Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I see your thought process, this is very close. Since you were nice enough to comply with the "hey, show us what you got" request, I'll make a few comments which I hope will be helpful for you in writing future code...

  • if (($ext =~ m/00./) and ($ext ne ".001")){ The first conditional as part of the "and" is not needed.
    ($ext ne ".001") says it all.
  • elsif ($ext eq ".001") { This "if" is not needed either. $ext has to be equal to ".001" if you get to this point. The previous lines have rejected any value that wasn't equal to "001".
  • Adding comment about:
    my $filenameroot = $file; $filenameroot =~ s/(.+)\.[^.]+$/$1/; # File name root

    This is fine, you make a copy of "$file" by assigning that to a new variable, "$filenameroot" Then you use a substitute operation to modify $filenameroot. This works. However consider:
    (my $filenameroot) = $file =~ m/(.+)\.[^.]+$/;
    In general a substitute operation is more "expensive" than a simple "return a value" operation. That is because the input string must be modified instead of selected parts just being copied. If you put the LHS (Left hand side) into a List context, you can assign $1, and even $2,$3.. from a match. Here $1 gets assigned to $filenameroot - no substitution operation required. This of course also avoids the problem of assigning $filenameroot to something that it is "not quite correct" yet. Here $filenameroot becomes $1.
  • my @list = glob("$folder$filenameroot*"); I am not sure if glob() returns a sorted list or not? Even if it does, it would be Character String sorted and not numerically sorted. This can make a big difference as "13" sorts lower than "3". This sorting difference between Character and Numeric is something to consider when you have numeric values. I don't know for sure whether this is a problem, but always include some double digit numbers in your test cases.
  • The big issue with the glob() is that you are re-reading the directory multiple times. File system operations are "expensive" in terms of CPU. Get in the habit of trying to do a directory read "only once". Store it if you have to in your own data structure. Of course in your application, I don't expect any performance issue, but this is something to be aware of in the future.
  • print "Last element : $list[(scalar @list-1)]\n"; That does indeed get the last element of @list. However there could be a problem because that last element might not be the file with the largest extension number due to previously mentioned potential sorting issues? Note better written as $list[-1]. In Perl the -1 index is the last item, -2 is next to last, etc. A very handy concept. Your code is correct, just mentioning that there is a better syntax for this.
  • 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).


    In reply to Re^3: Find the last item in a series of files by Marshall
    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 studying the Monastery: (5)
As of 2024-04-19 14:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found