in reply to converting "30k" string to integer

Perhaps not the most elegant, nor the most terrifying, but...
s((\d+)([kmg]b?)){ $1 * ({ k => 1000, kb => 1024, m => 1000*1000, mb => 1024*1024, g => 1000*1000*1000, gb => 1024*1024*1024, })->{lc $2} }ie;
Updated - For case insensitivity, I simply added an i modifier on the regex, and of course the lc on the $2.

jdporter
The 6th Rule of Perl Club is -- There is no Rule #6.

Replies are listed 'Best First'.
Re: Re: converting "30k" string to integer
by Anonymous Monk on May 16, 2003 at 12:32 UTC
    99.44% pure!

    I modded it to have ([kKmMgG]b?B?) and {lc($2)} for case insensitivity and it's just what I needed. Thanks!

    If speed becomes an issue, I'll check out the other suggestions here - thanks to all!

      You might want to use ([kKmMgG][bB]?) instead of ([kKmMgG]b?B?). Your version will recognize "3gbB" as well as "3gB", although the first version is not a key in the hash. Its undef value will be converted to 0, so that you will have that string replaced with "0".