[Israel.pm] A regexp question

Mikhael Goikhman migo at homemail.com
Mon May 17 07:51:05 PDT 2004


On 17 May 2004 16:33:15 +0300, Gaal Yahas wrote:
> 
> On Mon, May 17, 2004 at 01:04:59PM +0000, Mikhael Goikhman wrote:
> > On 17 May 2004 15:33:09 +0200, Itzik Lerner wrote:
> > >   $mac=~s/([\dA-Fa-f]{1,2})[:-]?/sprintf "%02X",$1/eg;
> > 
> > Two technical notes for those who want to learn.  The "[:-]?" substring
> > does not contribute anything useful in this example, and there is a bug,
> > a missing hex() function. The correct statement is:
> 
> 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.

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;

Regards,
Mikhael.

-- 
perl -e 'print+chr(64+hex)for+split//,d9b815c07f9b8d1e'



More information about the Perl mailing list