James Parsons wrote:
> Ok here go's , I'm just starting to Learn Perl and it's a very slow
> process.
>
> I have this script the calculates the sum a bunch of numbers and
> prints the final number to file, but my problem is I would like the
> number to have a floating $ sign but I'm sure how do this
>
> Any help would be great
>
>
> Script:
>
> #!/usr/bin/perl -w
>
> use strict;
>
> my $total = 0;
>
> open(FILE,"/usr/local/logs/pos/dollar_me") || die $!;
>
> $total += $_ while(<FILE>);
>
> close(FILE);
>
> printf ("%7.2f\n",$total);
>
>
> The number comes out as 120.23 instead of $120.23..
To my knowledge there's no built-in way of outputting floating
dollars, but since it's Perl you can do it any number of ways.
The function
printf ($format, @list)
is the same thing as
print (sprintf ($format, @list))
so the easiest way I can think of is to format the value and
add the dollar before you print it:
my $val = > sprintf "%7.2f", $total;
$val =~ s/\s\b/$/;
print $val, "\n";
which formats the value of $total into a string in $val,and
then changes the last space before the number into a dollar.
\s matches any single whitespace characters and \b is a
zero-length match on a word boundary, i.e. the beginning
of the number.
HTH,
Rob
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]