My attempt uses the fact that "gb" means "1024 ** 3", whereas "g" means "1000 ** 3", so there is just a diffrence in the bases, whereas the exponents are the same. I just store the single-letter exponent identifiers in a hash as keys with their corresponding exponents as values and do the same with the base identifiers ("" for decimal and "b" for binary) and then do the rest with a regex:
#!perl -ws my %exponents = ( 'k' => 1, 'm' => 2, 'g' => 3, 't' => 4 ); my %bases = ( '' => 1000, 'b' => 1024 ); while (<DATA>) { chomp; my $old = $_; s<(\d+)([a-zA-Z])([a-zA-Z]?)\b> < (defined $exponents{lc($2)} and defined $bases{lc($3)}) ? # suff +ix recognized ? $1 * ($bases{lc($3)} ** $exponents{lc($2)}) # substitute if y +es : "$1$2$3" # otherwise just +leave it >eg; print "`$old' became `$_'\n"; } __DATA__ My dog makes $30k a year. I own a 20gb hard drive. The budget deficite is $10g. 18h is 24d.
The output is as expected (the last data line does not change).

In reply to Re: converting "30k" string to integer by CombatSquirrel
in thread converting "30k" string to integer by dwhite20899

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.