Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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:  <%-{-{-{-<


In reply to Re^2: Concatenate and join by AnomalousMonk
in thread Concatenate and join by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-03-29 14:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found