$re='.*?(@)((?:[a-z][a-z0-9_]*))(\\[)(\\d+)(\\])'; #find arrays e.g: @arry[123] open(FILE, "+<", "xxxxx.pl"); @code = ; close FILE; $code = join ("",@code); foreach (@code) { if($_ =~ m/$re/) { #check if this line contains any array my $random_number = int(rand(9999)); #create a new index for that array $OldVariable = $1.$2.$3.$4.$5; #store the old array in variable $NewVariable = $1.$2.$3.$random_number.$5; #changing the index in the found array $code =~ s/$OldVariable/$NewVariable/g; #now replace all of that found array with the new one. } } print $code; <>;