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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.