in reply to substitute with sub result?

You need the e modifier:
$text =~ s/(\d+)/munge($1)/eg; ^ |

Also, it should be:

my $number = shift;

Update: Or, if you prefer engineering notation, you could try the "eng_not" sub below instead of your "munge". It's just something I have lying around:

sub eng_not { # Format a numeric value using engineering notation. # Examples: # 1234 --> 1.23k # 5e-5 --> 50u my $num = shift; my $mult; my $prefix; my $val; my $sign = ($num<0) ? '-' : ''; $num = abs $num; if ( ($num > 1e15) || ($num < 1e-15) ) { return sprintf '%3.2e', $n +um } if ($num > 1e12) { $prefix = 'T'; $mult = 1e12; } elsif ($num > 1e9) { $prefix = 'G'; $mult = 1e9; } elsif ($num > 1e6) { $prefix = 'M'; $mult = 1e6; } elsif ($num > 1e3) { $prefix = 'k'; $mult = 1e3; } elsif ($num > 1e0) { $prefix = '' ; $mult = 1e0; } elsif ($num > 1e-3) { $prefix = 'm'; $mult = 1e-3; } elsif ($num > 1e-6) { $prefix = 'u'; $mult = 1e-6; } elsif ($num > 1e-9) { $prefix = 'n'; $mult = 1e-9; } elsif ($num > 1e-12) { $prefix = 'p'; $mult = 1e-12; } $val = sprintf '%f', $num/$mult; $val = substr($val,0,4); # only want 3 digits and decimal point $val =~ s/0$//; # delete 1st trailing 0, if any $val =~ s/0$//; # delete 2nd trailing 0, if any $val =~ s/\.$//; # delete trailing decimal point, if any return $sign . $val . $prefix; }

Replies are listed 'Best First'.
Re^2: substitute with sub result?
by pileofrogs (Priest) on Mar 07, 2009 at 01:03 UTC

    Woah! Cool!

    Where is that documented? I don't see it in perlre or perlop.

    D'oh... oh yes it is...right there in perlop...

    Carry on.. nothing to see here...