Firstly, tell us what you are trying to do! By chopping $', you appear to want to match the "number" section of the file name (i.e., without the preceding 0s). Is this correct? If so, no need - 00001 == 1, you don't need to do a "if ('00001' =~ /0*/)" and use "$' == 1", if you see what I mean!!

Next, try not to use $' unless you have to. Bad practice, when a regex match does just as well. I would suggest something similar to:

$file =~ /^0000(?:0*)(.*)$/; $temp = $1;

.. to achieve your goal. I assume, from your regex, that you only want files with more than three preceding zeros. Mine does that, throws away any other preceding zeros, and passes the rest to $temp. Obviously, if it doesn't match then $temp is undef - don't unlink it :)

Now, you can do the comparison by treating the string as a hex number and then testing it. But, I notice A001 is on the end of both - is this common to all files? If so, you could change the regex above to /^0000(?:0*)(.*)A001$/, and then just treat $temp as a number, no fiddling.

Also, if you at all can, change the (.*) to something more strict - if you know that the file will have a five-digit number, for example, express that in the regex. And comment what the regex is _supposed_ to match!!


In reply to Re: Pattern Matching by kal
in thread Pattern Matching by JSchmitz

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.