in reply to $1[

It's not easy to understand what your question is, that's why you get fuzzy answers.

> I guess that perl tried to interpret $1[ as element of an array @1?


@1=a..c; my $a="012210"; $a =~ s/(.)/$1[$1]/g; print $a;


> 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?

Now the gory details ...

you said in one of your replies

> which looks to me as a bug.

nope, but IMHO it's ...

... flawed by concept:

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.

Not a bug

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!