while($row = $sth->fetchrow_arrayref) { #pick up the first row as an array ref $Replacement[12]=$row->[0]; #at this point you have one item from the sql query in your #Replacement array $Kine=~s/FLF(\d\d)/$Replacement[$1]/ge; #replace all of them items in $Kine