Carl Jolley wrote:

> On Fri, 15 Feb 2002, Dirk Bremer wrote:
> 
> 
>>$Bill, it was not you who made a mistake with the benchmark, it was I and in the 
>process learned a lot of new things about the
>>Benchmark module, which is a wonderful tool and should be used by anyone who is 
>interested in performance. This is my revised
>>zero-suppression routine:
>>
>>sub ZeroSuppress($;$)
>>{
>>    local $_ = $_[0];
>>
>>    # If the argument for the decimal point exists, insert a decimal point
>>    # at the specified location.
>>    s/(\d{$_[1]})$/\.$1/ if (@_ > 1 and $_[1] > 0);
>>
>>    # Zero-suppress the string.
>>    s/\b0+(?=\d)//;
>>
>>    # Remove leading spaces.
>>    s/([-+]?)\s*/$1/;
>>
>>    # Insert comma separators.
>>    1 while s/^([-+]?\d+)(\d{3})/$1,$2/;
>>
>>    return($_);
>>}
>>
>>If a second numeric argument is supplied, it will specify to where a decimal point 
>will be inserted into the resulting string, which
>>will be left-justified. This routine is very valuable to me for a program that 
>displays the values of a data file where the
>>money-type fields are plain strings. Any suggestions for improvements will be 
>welcomed.
>>
>>Dirk Bremer - Systems Programmer II - ESS/AMS  - NISC St. Peters
>>636-922-9158 ext. 652 fax 636-447-4471
>>
>>[EMAIL PROTECTED]
>>www.nisc.cc
>>
>>
> 
> Based on precedence rules, shouldn't:
> 
> s/(\d{$_[1]})$/\.$1/ if (@_ > 1 and $_[1] > 0);
> 
> be more correctly coded as:
> 
> s/(\d{$_[1]})$/\.$1/ if (@_ > 1 && $_[1] > 0);


Both '&&' and 'and' are lower prec than >, so either should work fine.


> Won't "and $_[1] > 0" be executed even if @_ == 1?
> OTOH, won't $_[1] > 0 be false when @_ > 1 is false,
> i.e. could not the condition be safely collapsed to:
> 
> if $_[1] > 0; ?


or better yet   if $_[1];  might be better to handle undefined avoiding
the numeric comparison to 0.

-- 
   ,-/-  __      _  _         $Bill Luebkert   ICQ=14439852
  (_/   /  )    // //       DBE Collectibles   Mailto:[EMAIL PROTECTED]
   / ) /--<  o // //      http://dbecoll.tripod.com/ (Free site for Perl)
-/-' /___/_<_</_</_     Castle of Medieval Myth & Magic http://www.todbe.com/

_______________________________________________
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to