Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^3: Corrupt Data?

by massa (Hermit)
on Jul 15, 2008 at 00:11 UTC ( [id://697600]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Corrupt Data?
in thread Corrupt Data?

I will (too) try to explain and detail almut's answer:
my @mac_addrs = ("0015FAA3F03A", "0015FAA3F03B", "0015FAA3F03C"); sub convert { return map { "AP" . join ".", unpack "(A4)*", lc $_ } @_; } print "$_\n" for convert(@mac_addrs);
Well:
  •  @answer = map { do_something_with("$_") } @array transforms one @array into another array @answer where each element of @answer is the result of the transform do_something_with(). Try this:
    $ perl -le '@a = (2, 3, 4, 5); @b = map { 2 * $_ } @a; print for @b' 4 6 8 10
    Notice that @b has each of the elements of @a, doubled. The result of the block given to map can be an array, too, so:
    $ perl -le '@a = (2, 3, 4, 5); @b = map { $_ % 2 ? () : ($_, $_ / 2) } + @a; print for @b' 2 1 4 2
    Notice that it printed the number and its half -- for the even numbers in @a.
  • So, let's try the knowledge of map:
    $ perl -le '@mac_addrs = ("0015FAA3F03A", "0015FAA3F03B", "0015FAA3F03 +C"); @a = map { lc $_ } @mac_addrs; print for @a' 0015faa3f03a 0015faa3f03b 0015faa3f03c
  • Now, let's add unpack"(A4)*" to the equation (unpack any number of groups of four alphanumeric chars):
    $ perl -le '@mac_addrs = ("0015FAA3F03A", "0015FAA3F03B", "0015FAA3F03 +C"); @a = map { unpack "(A4)*", lc $_ } @mac_addrs; print for @a' 0015 faa3 f03a 0015 faa3 f03b 0015 faa3 f03c
  • Now, let's join the parts with ".":
    $ perl -le '@mac_addrs = ("0015FAA3F03A", "0015FAA3F03B", "0015FAA3F03 +C"); @a = map { join ".", unpack "(A4)*", lc $_ } @mac_addrs; print f +or @a' 0015.faa3.f03a 0015.faa3.f03b 0015.faa3.f03c
  • And then, add the "AP" in the beginning:
    @mac_addrs = ("0015FAA3F03A", "0015FAA3F03B", "0015FAA3F03C"); @a = ma +p { "AP" . join ".", unpack "(A4)*", lc $_ } @mac_addrs; print for @a +' AP0015.faa3.f03a AP0015.faa3.f03b AP0015.faa3.f03c
  • voilą!! you already have your answer. Now, package everything in a neat subroutine:
    $ perl -le ' > use strict; > my @mac_addrs = ("0015FAA3F03A", "0015FAA3F03B", "0015FAA3F03C"); > sub convert { > map { "AP" . join ".", unpack "(A4)*", lc $_ } @_ > } > print for convert @mac_addrs > ' AP0015.faa3.f03a AP0015.faa3.f03b AP0015.faa3.f03c
Got it?
[]s, HTH, Massa

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2024-03-28 19:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found