[ create a new paste ] login | about

Link: http://codepad.org/XSbDel34    [ raw code | output | fork ]

vol7ron - Perl, pasted on Oct 14:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    sub phoneFormat{
        my @sigils  = ('+','/','-');            # joiners
        $_ = reverse(shift);                    # input; reversed for matches
        @_ = grep{defined} unpack "A4A3A3A1",$_;    # match, starting with extension; and remove unmatched
        $_ = join('', map {$_ . pop @sigils } @_ ); # add in the delimiters
        ($_ = reverse) =~ s/^[^\d]+//;          # reverse back and remove leading non-digits
        $_;
    }

    print phoneFormat('012')         , "\n";    # (blank)
    print phoneFormat('0123')        , "\n";    # 0123
    print phoneFormat('0123456')     , "\n";    # 012-3456
    print phoneFormat('0123456789')  , "\n";    # 012/345-6789
    print phoneFormat('01234567899') , "\n";    # 0+123/456-7899
    print phoneFormat('012345678999') , "\n";   # 1+234/567-8999


Output:
1
2
3
4
5
6
012
0123
012-3456
012/345-6789
0+123/456-7899
1+234/567-8999


Create a new paste based on this one


Comments: