[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] A parseing we will go....



Allan,

> 001254.6987 or 0.2500 or 012.685000
> There's any number of leading or trailing zeroes before and after the
> decimal I want to get rid of.
> If they are all zeroes I need 1.0 instead of 1.00000 and 0.1 instead of
> 00000.1. Otherwise I just need to lop off the leaders or trailers.
> Any takers?

Paul Schinder had already proposed a solution using regexes; an alternative
is to force numeric context, as per 'Effective Perl Programming' (
Hall/Schwartz ) pp19-21.

# ---------- CUT HERE -----------------
#! /usr/bin/perl
use strict;

while( <main::DATA> ) {
    print 'In: ', $_, 'Out: ';
    print 0 + $_; # Adding zero forces numeric context
    print "\n";
}
__DATA__
001254.6987
0.2500
012.685000
# ---------- CUT HERE -----------------

This gives:
perl -w convnum.pl
In: 001254.6987
Out: 1254.6987
In: 0.2500
Out: 0.25
In: 012.685000
Out: 12.685

Thanks,

Steve Cardie
Pindar Systems plc


# ===== Want to unsubscribe from this list?
# ===== Send mail with body "unsubscribe" to macperl-request@macperl.org