Keep It Simple, Stupid | |
PerlMonks |
in reply to $1[
> I guess that perl tried to interpret $1[ as element of an array @1?
yes
@1=a..c; my $a="012210"; $a =~ s/(.)/$1[$1]/g; print $a;
abccba
> So I solved this problem in two ways, using ...
Excellent you even solved it, so what is the question again? :)
Your title $1[ is - sorry - really crap, please see How do I compose an effective node title? for inspiration.
And your question is obscured in between lots of regex line noise, please see How do I post a question effectively?
you said in one of your replies
> which looks to me as a bug.
nope, but IMHO it's ...
Normally an identifier in Perl has to match something like /[_a-zA-Z^][_a-zA-Z0-9]*/ but special variables like $\ or $1 are exceptions (see perlvar#SPECIAL-VARIABLES for a list)
They are global symbols and don't need to be declared.
Unfortunately this covers other possible slots of such a symbol, like %hash, @array, $scalar, &code, *glob and probably also filehandle and format....
(see perlmod#Symbol-Tables and perlref#Making-References point 7 for details)
For instance %\ doesn't have any meaning, but since $\ is a special variable, it is
I'd rather prefer that undefined special vars fail under strict, alas, we are talking about a language which supported this concept already in Perl4, i.e. long before it introduced lexicals and strict.
Since this /$array[/ is a parsing error and since 1 is not less a symbol than array it's not a bug.
This is normally not a problem, but regex' tend to become messy.
Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Je suis Charlie!
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: $1[ (or "Does an array @1 exist in Perl ? - Yes!")
by rsFalse (Chaplain) on Oct 11, 2017 at 14:38 UTC | |
by davido (Cardinal) on Oct 11, 2017 at 15:52 UTC | |
by hippo (Bishop) on Oct 11, 2017 at 15:14 UTC | |
by LanX (Saint) on Oct 11, 2017 at 15:31 UTC | |
by rsFalse (Chaplain) on Oct 28, 2017 at 13:30 UTC | |
by LanX (Saint) on Oct 28, 2017 at 13:55 UTC | |
| |
by LanX (Saint) on Oct 11, 2017 at 15:08 UTC |