Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^2: Concatenate and join

by AnomalousMonk (Chancellor)
on Mar 31, 2016 at 20:55 UTC ( #1159229=note: print w/replies, xml ) Need Help??


in reply to Re: Concatenate and join
in thread Concatenate and join

Dunno if this is too/more/less ugly, but anyway...
File: convert_mac_addr_2.pl

use 5.010; # needs \K \g-1 use warnings; use strict; use Test::More 'no_plan'; use Test::NoWarnings; VECTOR: for my $ar_vector ( "ALL of these shall be converted", [ '28:8a:1c:59:cc:85' => '288a.1c59.cc85' ], [ '28-8a-1c-59-cc-85' => '288a.1c59.cc85' ], "NONE of these shall be converted", map [ ($_) x 2 ], # string unchanged, so input expected as output # inconsistent separator usage qw(28:8a-1c-59-cc-85 28-8a-1c-59-cc:85 28-8a:1c:59:cc:85 28:8a:1c:59:cc-85 28:8a:1c-59:cc:85 28-8a-1c:59-cc-85 ), # too few/too many/malformed octets qw(28:8a:1c:59:cc:8 28:8a:1c:59:cc: 28-8a-1c-59-cc 28-8a-1c-59-cc-85- 28-8a-1c-59-cc-85-8 28:8a:1c:59:cc:85:88 28:8a:1c:59:cc:85:888 -8a-1c-59-cc-85 8-8a-1c-59-cc-85 228-8a-1c-59-cc-85 -28-8a-1c-59-cc-85 28:8a:1g:59:cc:85:88 28:8a:1:59:cc:85:88 28:8a:111:59:cc:85:88 ), ) { if (not ref $ar_vector) { note $ar_vector; next VECTOR; } my ($mac, $expected) = @$ar_vector; my $xx = qr{ [[:xdigit:]]{2} }xms; my $sep = qr{ [-:] }xms; my $converted = (my $final = $mac) =~ s{ \A $xx \K ($sep) (?= (?: $xx \g-1){4} $xx \z) | \G (?! \A) $xx \K $sep } {@{[ '', '.' ]}[ $-[0] % 2 ]}xmsg; $converted = $converted ? 'converted' : 'NO conversion'; is $final, $expected, qq{'$mac' -> '$final' ($converted)}; } # end for VECTOR done_testing;
Output:
c:\@Work\Perl\monks\Anonymous Monk\1159176>perl convert_mac_addr_2.pl # ALL of these shall be converted ok 1 - '28:8a:1c:59:cc:85' -> '288a.1c59.cc85' (converted) ok 2 - '28-8a-1c-59-cc-85' -> '288a.1c59.cc85' (converted) # NONE of these shall be converted ok 3 - '28:8a-1c-59-cc-85' -> '28:8a-1c-59-cc-85' (NO conversion) ok 4 - '28-8a-1c-59-cc:85' -> '28-8a-1c-59-cc:85' (NO conversion) ok 5 - '28-8a:1c:59:cc:85' -> '28-8a:1c:59:cc:85' (NO conversion) ok 6 - '28:8a:1c:59:cc-85' -> '28:8a:1c:59:cc-85' (NO conversion) ok 7 - '28:8a:1c-59:cc:85' -> '28:8a:1c-59:cc:85' (NO conversion) ok 8 - '28-8a-1c:59-cc-85' -> '28-8a-1c:59-cc-85' (NO conversion) ok 9 - '28:8a:1c:59:cc:8' -> '28:8a:1c:59:cc:8' (NO conversion) ok 10 - '28:8a:1c:59:cc:' -> '28:8a:1c:59:cc:' (NO conversion) ok 11 - '28-8a-1c-59-cc' -> '28-8a-1c-59-cc' (NO conversion) ok 12 - '28-8a-1c-59-cc-85-' -> '28-8a-1c-59-cc-85-' (NO conversion) ok 13 - '28-8a-1c-59-cc-85-8' -> '28-8a-1c-59-cc-85-8' (NO conversion) ok 14 - '28:8a:1c:59:cc:85:88' -> '28:8a:1c:59:cc:85:88' (NO conversio +n) ok 15 - '28:8a:1c:59:cc:85:888' -> '28:8a:1c:59:cc:85:888' (NO convers +ion) ok 16 - '-8a-1c-59-cc-85' -> '-8a-1c-59-cc-85' (NO conversion) ok 17 - '8-8a-1c-59-cc-85' -> '8-8a-1c-59-cc-85' (NO conversion) ok 18 - '228-8a-1c-59-cc-85' -> '228-8a-1c-59-cc-85' (NO conversion) ok 19 - '-28-8a-1c-59-cc-85' -> '-28-8a-1c-59-cc-85' (NO conversion) ok 20 - '28:8a:1g:59:cc:85:88' -> '28:8a:1g:59:cc:85:88' (NO conversio +n) ok 21 - '28:8a:1:59:cc:85:88' -> '28:8a:1:59:cc:85:88' (NO conversion) ok 22 - '28:8a:111:59:cc:85:88' -> '28:8a:111:59:cc:85:88' (NO convers +ion) 1..22 ok 23 - no warnings 1..23


Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1159229]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2018-10-19 08:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When I need money for a bigger acquisition, I usually ...














    Results (107 votes). Check out past polls.

    Notices?