[Israel.pm] A regexp question

Gaal Yahas gaal at forum2.org
Mon May 17 08:16:03 PDT 2004


On Mon, May 17, 2004 at 02:51:05PM +0000, Mikhael Goikhman wrote:
> > FWIW, Perl allows a templated %s, so you can skip the call to hex:
> > 
> >      $mac=~s/([\dA-Fa-f]{1,2})[:-]?/sprintf "%02s",$1/eg;
> 
> Yes of course, but the canonical form may want to only use lowercased
> or uppercased hex digits, this is why I used "%X" + hex.

In that case, as a matter of style I'd go for \U or uc etc, which
make the canonization purpose more explicit (and are even a bit faster).

> And if you want the shortest code (not necessarily the clearest), here is
> a solution I composed yesterday, with no sprintf. Remove spaces for golf:
> 
>   $m16 = join '', map { s/^.$/0$_/; $_ } split /[:-]/, $mac;

You forgot the ':' in the join, but yes, I had about the same thing
in mind.

-- 
Gaal Yahas <gaal at forum2.org>
http://gaal.livejournal.com/



More information about the Perl mailing list