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); 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; ? Also, in the replacement part of the substitute statement, although it's allowed, it should not be required that you escape the decimal point. The . character as a regex meta character only has regex metacharacter meaning in the match part of a regex. **** [EMAIL PROTECTED] <Carl Jolley> **** All opinions are my own and not necessarily those of my employer **** _______________________________________________ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs