sub getArray { my $n = $_[0]; my @examples = ( "The population of the U.S. is ${\($n*340389145)} people", "The frequency is ${\($n*13824)}Hz", "This is a real number, ${\($n*723540.34034)}", "${\($n*1045.357)}", "${\($n*834569.334656)}", "${\($n*1000000)} B.C.", "My daily compensation is \$${\($n*2234.36)} per hour.", "${\($n*2349134)}", "${\($n*123456)}", ); return @examples; } my $n = 4; my @have = getArray(1); my @want = getArray($n); for my $i (0 .. $#have) { my $have = $have[$i]; # say "$have"; my $want = $want[$i]; # say "$want"; (my $work1 = (($have) =~ s{ \d+\.?\d* }{ $& * $n }erx )) =~ s{ \d+ }{ $& =~ s/ (?<=\d) (?= (?:\d{3} )+ (?!\d) ) /,/xrg; }ex; (my $work2 = $want) =~ s{ (? (\d+)) }{ $+{int} =~ s/ (?<=\d) (?= (?:\d{3} )+ (?!\d) ) /,/xrg; }ex; is $work1, $work2, 'Factor and insert commas where appropriate'; }