Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Concatenate and join

by choroba (Cardinal)
on Mar 31, 2016 at 14:02 UTC ( [id://1159203]=note: print w/replies, xml ) Need Help??


in reply to Concatenate and join

Another solution (TIMTOWTDI), using pack and unpack:
#!/usr/bin/perl use warnings; use strict; my $mac_address = '28:8a:1c:59:cc:85'; $mac_address =~ tr/://d; print join '.', unpack '(H4)*', pack 'H*', $mac_address;

Update: Could anyone use pack to skip the colons, without the need to remove them beforehand?

($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Replies are listed 'Best First'.
Re^2: Concatenate and join
by Marshall (Canon) on Mar 31, 2016 at 14:39 UTC
    Interesting. I normally only think of pack and unpack in the context of working with binary buffers, which I seldom do in Perl.

    While researching regex ideas, I did see your post at Re: Insert colons into a MAC address which is sort of similar to this problem, but I couldn't see how to apply it to leave off the final "." in my regex.

      It's not possible to use the same trick. You want to concatenate the two groups every time, but skip the dot sometimes. Maybe with eval:
      s/(\w+):(\w+)(:?)/ $1 . $2 . '.' x !!$3 /ge;

      or use two steps (which makes it similar to your solution):

      s/(\w+):(\w+)/$1$2/g; tr/:/./;

      ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2024-04-25 18:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found