in reply to Replace arrays in external file

You have to replace:  if (/$re/) {...}

with :  while (/$re/g) {...}

-----
use strict; my $re='.*?(@)((?:[a-z][a-z0-9_]*))(\\[)(\\d+)(\\])'; my @code = <DATA>; my $code; foreach (@code) { $code .= $_; while (/$re/g) { my $rndIndex; my $c1=$1;my $var1=$2;my $index=$4;my $c3=$5; $rndIndex = int(rand(99999)); my $old = $c1.$var1.$index.$c3; my $varName = $index.$c3; $varName =~ s/$index/$rndIndex/; $code =~ s/[$old]/$varName/g; } } print $code; #show output example __DATA__ @a = ("'hello world'",'print ', ';'); eval(@a[1] . @a[0] . @a[2]);

Update: You don't have to capture "@", "[", "]"... plus several simplifications.

use strict; my $code; { #slurp mode (http://perldoc.perl.org/perlvar.html) local $/; $code = <DATA>; } my $re = qr {\@([a-z][a-z0-9_]*)\[\d+]}; $code =~ s{$re}{ "\@$1\[" . int(rand(99999)) . "]" }eg; print $code; #show output example __DATA__ @a = ("'hello world'",'print ', ';'); eval(@a[1] . @a[0] . @a[2]);