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

Re: Concatenate and join

by Marshall (Canon)
on Mar 31, 2016 at 10:24 UTC ( [id://1159189]=note: print w/replies, xml ) Need Help??


in reply to Concatenate and join

I think it is easier to calculate "final mac" directly from mac. I used a regex, but I couldn't figure out how to write it so that a final "." didn't appear at the end. I finally gave up and just ran another simple regex to ditch this unintended side effect at the end. I highly suspect that a more advanced regex could have avoided this, but sometimes straightforward is a good answer.

The intermediate atmac is not needed but is easy to calculate with tr. I'm not sure whether or not all 4 of these variants are needed? Or why atmac is in an array. But I figured that "final mac" was the most important.

Anyway another idea for you...

#!/usr/bin/perl use warnings; use strict; my $mac = '28:8a:1c:59:cc:85'; (my $finalmac = $mac) =~ s/(\w+):(\w+)(:?)/$1$2./g; $finalmac =~ s/.$//; #ditch "." at end (my $atmac = $mac) =~ tr/:/ /; print "mac = $mac\n"; print "atmac = $atmac\n"; print "final mac = $finalmac\n"; __END__ mac = 28:8a:1c:59:cc:85 atmac = 28 8a 1c 59 cc 85 final mac = 288a.1c59.cc85
Update: I just noticed the /o in your regex code. Modern Perl doesn't need this hint. I remember bench marking a lot of stuff years ago with Perl 5.10, and I didn't see any difference. Also see Perl regex documentation 5.22, " o - pretend to optimize your code, but actually introduce bugs". I would pay attention to a cautionary note like that!

Replies are listed 'Best First'.

Log In?
Username:
Password:

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

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

    No recent polls found