G'day insta.gator,
If you're using Perl v5.14 (or later), you can do all those operations in one statement.
I've chosen transliteration (y///), for the '$' and ',' removal, because it's quicker than substitution (s///); although, you could stick with substitution if you want.
If you do stay with s///, consider the character class '[\$,]' in favour of the alternation '\$|,'. I expect, but don't know for certain, that the former would quicker: Benchmark if you're interested.
Anyway, v5.14 introduced the '\r' modifier which allows you to chain s/// and y/// operations. Here's my test code:
#!/usr/bin/env perl use v5.14; use warnings; my @TFCNTL_Tax; while (<DATA>) { $TFCNTL_Tax[@TFCNTL_Tax] = (split)[8] =~ y/$,//dr =~ s/^(.*)-$/-$1 +/r; } say for @TFCNTL_Tax; __DATA__ 0 0 0 0 0 0 0 0 $1,234.56 0 0 0 0 0 0 0 0 0 $7,890.12- 0 0 0 0 0 0 0 0 0 -$3,456.78 0 0 0 0 0 0 0 0 0 $-9,012.34 0
Output:
1234.56 -7890.12 -3456.78 -9012.34
If you chain two substitutions instead:
$TFCNTL_Tax[@TFCNTL_Tax] = (split)[8] =~ s/[\$,]//gr =~ s/^(.*)-$/ +-$1/r;
the output remains the same.
See also: perlre: Modifiers (for /r); perlop: Quote-Like Operators (for y///); perlop: Regexp Quote-Like Operators (for s///).
-- Ken
In reply to Re: Proper creation of a negative number
by kcott
in thread Proper creation of a negative number
by insta.gator
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |