Re: failed substitution
- From: info@xxxxxxxxxxxx (D. Bolliger)
- Date: Tue, 28 Nov 2006 12:39:13 +0100
Beginner am Dienstag, 28. November 2006 11:55:
[snipped for brevity, sorry]
Thanx Dani and John,
I should have realised that the that I was making the substitiution
on the full path and not the basename.
I appreciate you showing me how to shorten the code. Can I ask if I
am reading it right.
foreach my $basef (map basename ($_), @files) {
(my $l) = ($basef =~ /([a-z]{1,2})\.jpg$/);
Does this basename everything in @files and make it $basef?
Yes, every file in @files is "piped" through map witch applies the basename
function, and the result is stored in $basef, used within the foreach loop.
For the powerful map function see:
perldoc -f map
In John's example I am not sure what is happening with this RegEx:
( my $new = $f ) =~ s/([a-z]{1,2})(?=\.jpg\z)/_a/;
First, $f is copied into $new and the regex is applied to $new.
(?=something_here) is a positive lookahead not actually matching
something_here. The regex sais:
"match one or two a-z chars that are followed by the string '.jpg', and
replace this/these char(s) with '_a'". See
perldoc perlre
There are 2 sets of parentheses but one lvalue, $new. So is that any
character a-z, 1 or 2 times and the ? mean 1 or more times?
No, the question mark is part of the '(?=)' construct, all described in
perlre.
What is the \z switch here? I can find it is perlre.
It's under the paragraph "Perl defines the following zero-width assertions"
(btw, at least on linux, you can call the search funktion by typing a '/'
while viewing)
Dani
.
- References:
- failed substitution
- From: Beginner
- Re: failed substitution
- From: D. Bolliger
- Re: failed substitution
- From: Beginner
- failed substitution
- Prev by Date: Re: What's this string?
- Next by Date: RE: write, print, & Tk::Text
- Previous by thread: Re: failed substitution
- Next by thread: Re: failed substitution
- Index(es):