This worked with the sample data. It uses strict and warnings. There are 5 additional lines and changes to 4 lines. Otherwise, the code is the same as yours. Not saying there may be better ways. :-)
#!/usr/bin/perl
use strict;
use warnings;
my @data; # new addition
my $max = 0; # new addition
while (<DATA>) {
chomp; # new change
next unless /\S/; # new change
my $string = $_; # new change
$string =~ s/,000//g;
$string =~ s/,//g;
my @numbers= $string =~ /(\d+)/g;
my $f= $numbers[0];
my $s= $numbers[1] || 0; # new change
my $isf9 = substr($f,-3);
if ($isf9 =~/999/) {$f=$f + 1;}
my $iss9 = substr($s,-3);
if ($iss9 =~/999/) {$s=$s + 1;}
$f =~ s/000//g;
$s =~ s/000//g;
my $fac;
if ($f>0 && $s>0){ $fac = (($s-$f)/2)+$f;}
if ($f>0 && $s<1){ $fac = $f;}
if ($fac>0)
{
my $length = length($fac); # new addition
$max = $length if $length > $max; # new addition
$_ =~ s/^/($fac) /;
$_ =~ s/$/;fac=$fac/;
}
push @data, $_;
}
# new addition
print map {s/\((.+?)\)/ sprintf "(%$max.1f)", $1/e; "$_\n"} @data;
__DATA__
Less than $10,000
$10,000 to less than $20,000
$20,000 to less than $30,000
$30,000 to less than $40,000
$40,000 to less than $50,000
$50,000 to less than $60,000
$60,000 to less than $70,000
$70,000 to less than $80,000
$80,000 to less than $90,000
$90,000 to less than $100,000
$100,000 to less than $125,000
$125,000 to less than $150,000
$150,000 or higher
|