[Chicago-talk] Removing "." from a string?

Steven Lembark lembark at wrkhors.com
Thu Jan 31 12:14:06 PST 2008


Richard Reina wrote:
> Does anyone know an easy way to remove "."s from a string like
> 192.168.2.16
>
> so I get 192168216?  Substring seems cumbersome and does not account for
> length between dots.
>
> Thanks for any help.

Here are four progessively more ridiculous solutions:

    $dotted_quad    =~ s/\D//g;

    my $digits      = join '', split /\D+/, $dotted_quad;

    my $digits      = join '', $dotted_quad =~ m{ (\d+) }gx;

    while( ( my $i = index $dotted_quad, '.' ) > 0 )
    {
        substr $dotted_quad, $i, 1, '';
    }

though the last one may actually be the fastest
due to the lack of recursion and extra data
structures.


-- 
Steven Lembark                                          +1 888 359 3508
Workhorse Computing                                       85-09 90th St
lembark at wrkhors.com                                 Woodhaven, NY 11421


More information about the Chicago-talk mailing list