[Hartford-pm] Re: [Boston.pm] sprintf sign handling question?
Uri Guttman
uri at stemsystems.com
Tue Jun 22 22:21:07 CDT 2004
>>>>> "BM" == Bob Mariotti <r.mariotti at financialdatacorp.com> writes:
BM> I have a perl program that extracts data from a report file. Some of
BM> the fields (columns) are monetary with a trailing sign (i.e:
BM> 1,234.56-).
BM> I extract the fields with the unpack function into scalars then strip
BM> out the commas. If the field ends with a '-' char then I subtract the
BM> scalar from zero placing it back into itself.
why do you need a true negative number? it looks like all you are doing
is format stuff.
BM> OK - I am getting what looks like a properly formatted file. However,
BM> I am having problems with sign handling. Can anyone please offer
BM> suggestions to help me arrive at the following:
BM> input field: 1,234.56-
BM> getting output: -00123456
BM> Desired output: 00123456-
what do you do if the number is positive? a trailing blank?
how long is the output field? i will assume 9 chars and padded with
leading 0's as you show above
this could do just that part if you want:
my $num = '1,234.56-' ;
$num =~ tr/,.//d ;
$out = substr( '0' x 9 . $num, -9, 9 ) ;
BM> or preferably: x'30303132333435d6' which is a zoned ascii decimal
BM> field with a negating overstrike on the low order byte.
that is a little trickier but doable. again, i assume 8 bytes this time
and we have already stripped the exising - char. the number in use is
positive here but we know to mung the sign byte later.
$out = sprintf( "%08d", $num ) | "\0" x 7 . "\xd0" ;
hope that helps,
uri
--
Uri Guttman ------ uri at stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
More information about the Hartford-pm
mailing list