Some further thoughts on this.

If $pref is supposed to hold 001, you're removing all digits from it with:

$pref =~ s/[0-9]+//gi;

I'm wondering if that substitution is supposed to be the same as the previous two (i.e. s/[^0-9]+//gi). If not, the 'i' (case insensitivity) modifier is pointless.

On a more general point regarding those substitutions, s/chars//g is a slow way to do it and s/chars//gi is even slower. A transliteration (or, in this case, transobliteration) is faster. See the "Search and replace or tr" section of "Perl Performance and Optimization Techniques".

You have six instances in your code. Here's how you could replace them:

$ perl -le 'my $x = "1q2w3e"; $x =~ y/0-9//cd; print $x' 123
$ perl -le 'my $x = "1q2w3e"; $x =~ y/0-9//d; print $x' qwe
$ perl -le 'my $x = "ÞORNþorn"; $x =~ y/Þþ//d; print $x' ORNorn

In case you didn't know, y/// and tr/// are synonymous. See "perlop: Quote-Like Operators" for more details.

-- Ken


In reply to Re: Traverse directory backwards based on file by kcott
in thread Traverse directory backwards based on file by subhash1198

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.