in reply to Traverse directory backwards based on file
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Traverse directory backwards based on file
by subhash1198 (Initiate) on Mar 30, 2014 at 03:30 UTC | |
by kcott (Archbishop) on Mar 30, 2014 at 12:56 UTC | |
by subhash1198 (Initiate) on Mar 30, 2014 at 13:42 UTC | |
by kcott (Archbishop) on Mar 30, 2014 at 15:39 UTC |