Better ways to... 
Author Message
 Better ways to...

I'm looking for other ways to add colons to a MAC address.

Here's how I'm doing this:


print "$fmac\n";

The print gives me "12:34:45:78:9a:bc"  (unless I missed some syntax
above).  Any suggestions on how I could do this differently (more
efficiently, faster, simpler, etc).

Thanks for your suggestions.
Shetal

--
 ____________________________________________________________________
|                                                                    |

|                Don't wait for someone else to make                 |
|               your life terrific.  That's your job.                |
|____________________________________________________________________|
                  <Insert Standard Disclaimer Here>



Sat, 15 Apr 2000 03:00:00 GMT  
 Better ways to...

Quote:

>I'm looking for other ways to add colons to a MAC address.

>Here's how I'm doing this:
>$fmac="123456789abc";


>print "$fmac\n";

this is the same problem as adding commas to a number, which is
answered in the FAQ.  Jeffery Freidl also discusses the problem
at some length in "Mastering Regular Expressions" [1]

good luck :)

[1] see pages 229 and 292
Mastering Regular Expression
Jeffrey Freidl
ISBN 1-56592-257-3
<URL:http://www.oreilly.com>

--

NY.pm - New York Perl M((o|u)ngers|aniacs)*  <URL:http://ny.pm.org/>
CGI Meta FAQ <URL:http://computerdog.com/CGI_MetaFAQ.html>



Sat, 15 Apr 2000 03:00:00 GMT  
 Better ways to...

On Tue, 28 Oct 1997 05:32:49 -0400,

Quote:


> >I'm looking for other ways to add colons to a MAC address.
> this is the same problem as adding commas to a number, which is
> answered in the FAQ.

Not really.  The MAC address is fixed length (or ought to be,
considering the solution snpatel has come up with already), whereas the
comma problem is tricky because it is a variable length problem.

Try:

$fmac =~ s/(\S\S)(\S\S)(\S\S)(\S\S)(\S\S)(\S\S)/$1:$2:$3:$4:$5:$6/;

--



Sat, 15 Apr 2000 03:00:00 GMT  
 Better ways to...

Hi,

Quote:

> I'm looking for other ways to add colons to a MAC address.

> Here's how I'm doing this:
> $fmac="123456789abc";


> print "$fmac\n";

> The print gives me "12:34:45:78:9a:bc"  (unless I missed some syntax
> above).  Any suggestions on how I could do this differently (more
> efficiently, faster, simpler, etc).

Maybe you like this one:

   $fmac =~ s/(\S{2})(?!$)/$1:/g;

BTW: I would also replace '\S' by '[0-9a-f]'.

Bye, Eike
--
=======================================================================
-----------------------------------------------------------------------
 Home Page, Address, PGP,...:  http://www.phy.uni-bayreuth.de/~btpa25/
-----------------------------------------------------------------------
 PGP fingerprint:      1F F4 AB CF 1B 5F 4B 1D 75 A1 F9 C5 7B 3F 37 06
=======================================================================



Sat, 15 Apr 2000 03:00:00 GMT  
 Better ways to...


Quote:
>On Tue, 28 Oct 1997 05:32:49 -0400,



>> >I'm looking for other ways to add colons to a MAC address.

>> this is the same problem as adding commas to a number, which is
>> answered in the FAQ.

>Not really.  The MAC address is fixed length (or ought to be,
>considering the solution snpatel has come up with already), whereas the
>comma problem is tricky because it is a variable length problem.

the solution of the FAQ still works (the MAC address is a subset of
the set of strings for that problem).  the general solution of adding
a certain character after N characters works for the specific
examples too.   perl doesn't care what your data mean - Perl just does
what you tell it to do. :)

--

NY.pm - New York Perl M((o|u)ngers|aniacs)*  <URL:http://ny.pm.org/>
CGI Meta FAQ <URL:http://computerdog.com/CGI_MetaFAQ.html>



Sat, 15 Apr 2000 03:00:00 GMT  
 Better ways to...

If you do that, you should add this line:

chop($mac);

If you don't you'll end up with 12:34:56:78:9a:bc: (with a colon at the end)
which may or may not work for what you had in mind.

--- Creede Lambard
Minister of Irregular Expressions
Programming Republic of Perl

Quote:

>Hi,


>> I'm looking for other ways to add colons to a MAC address.

>> Here's how I'm doing this:
>> $fmac="123456789abc";


>> print "$fmac\n";

>> The print gives me "12:34:45:78:9a:bc"  (unless I missed some syntax
>> above).  Any suggestions on how I could do this differently (more
>> efficiently, faster, simpler, etc).

>Maybe you like this one:

>   $fmac =~ s/(\S{2})(?!$)/$1:/g;

>BTW: I would also replace '\S' by '[0-9a-f]'.

>Bye, Eike
>--
>=======================================================================

>-----------------------------------------------------------------------
> Home Page, Address, PGP,...:  http://www.phy.uni-bayreuth.de/~btpa25/
>-----------------------------------------------------------------------
> PGP fingerprint:      1F F4 AB CF 1B 5F 4B 1D 75 A1 F9 C5 7B 3F 37 06
>=======================================================================



Sat, 15 Apr 2000 03:00:00 GMT  
 Better ways to...

On Tue, 28 Oct 1997 06:27:25 -0800, "Creede Lambard"

Quote:

>If you do that, you should add this line:

>chop($mac);

>If you don't you'll end up with 12:34:56:78:9a:bc: (with a colon at the end)
>which may or may not work for what you had in mind.

>--- Creede Lambard
>Minister of Irregular Expressions
>Programming Republic of Perl

And if you do that, you'll wind up with:

Name "main::mac" used only once: possible typo at macaddr.pl line xx

Once you fix that, you'll wind up with 12:34:56:78:9a:b (without a c at the
end).  Eike's RE works fine . . . the zero-width negative lookahead assertion
"(?!$)" takes care of the last octet. Really. I tried it.

--
Jeffrey R. Drumm, Systems Integration Specialist
Maine Medical Center - Medical Information Systems Group
420 Cumberland Ave, Portland, Maine 04101



Sat, 15 Apr 2000 03:00:00 GMT  
 Better ways to...

Hi,

Quote:

> If you do that, you should add this line:

> chop($mac);

> If you don't you'll end up with 12:34:56:78:9a:bc: (with a colon at the end)
> which may or may not work for what you had in mind.

Really ? Did you try out my code ?

Note the '(?!$)' in my regular expression - it tests if the two
matched characters are at the end ('$') of the string or not. Only in
the latter case the substitution is done.

Quote:
> >   $fmac =~ s/(\S{2})(?!$)/$1:/g;

                        ^^^^^

Bye, Eike
--
=======================================================================
-----------------------------------------------------------------------
 Home Page, Address, PGP,...:  http://www.phy.uni-bayreuth.de/~btpa25/
-----------------------------------------------------------------------
 PGP fingerprint:      1F F4 AB CF 1B 5F 4B 1D 75 A1 F9 C5 7B 3F 37 06
=======================================================================



Sat, 15 Apr 2000 03:00:00 GMT  
 Better ways to...

On Tue, 28 Oct 1997 10:27:36 -0400,

Quote:
> the solution of the FAQ still works [...]

I didn't say it wouldn't.  I just said it was overkill.

--



Sat, 15 Apr 2000 03:00:00 GMT  
 Better ways to...


Quote:
> > I'm looking for other ways to add colons to a MAC address.
> > $fmac="123456789abc";


> Maybe you like this one:

>    $fmac =~ s/(\S{2})(?!$)/$1:/g;

     $fmac =~ s/(\S\S)\B/$1:/g;

Fewer keystrokes required; backwards compatible to perl 4.

Elijah
------
perl -e 's Y Yreverse q N ny pm ,srekcah lrep kroy wen emosNYex and s Pmp ynP
P and s MsMjust sMx and print and s NYPM MPYN Nis or reverse and print q q q'



Sat, 15 Apr 2000 03:00:00 GMT  
 Better ways to...

In article (Shetal N. Patel) writes:

   Here's how I'm doing this:
   $fmac="123456789abc";


   print "$fmac\n";

   The print gives me "12:34:45:78:9a:bc"  (unless I missed some syntax
   above).  Any suggestions on how I could do this differently (more
   efficiently, faster, simpler, etc).

Simpler :

machine% echo "12ASD3456789" | perl -pe 's/(\S{2})/$1:/g'
12:AS:D3:45:67:89:
machine%
--

* http://universe.pc.helsinki.fi/~matti/contact.html *
* +358-(0)40-593 50 91 but try first +358-(0)9-191 23978 *



Sat, 15 Apr 2000 03:00:00 GMT  
 Better ways to...

Quote:

> Fewer keystrokes required; backwards compatible to perl 4.

Eli, while I appreciate the former, the later is irrelevant except as a
historical oddity.

- doug

PS - Why do you cross-post everything to your fan group?  Just to make
sure you don't miss anything here?



Sun, 16 Apr 2000 03:00:00 GMT  
 Better ways to...

Matti Kinnunen wrote in article ...

Quote:
>In article (Shetal N. Patel) writes:

>   Here's how I'm doing this:
>   $fmac=3D"123456789abc";


>   print "$fmac\n";

>   The print gives me "12:34:45:78:9a:bc"  (unless I missed some syntax
>   above).  Any suggestions on how I could do this differently (more
>   efficiently, faster, simpler, etc).

>Simpler :

>machine% echo "12ASD3456789" | perl -pe 's/(\S{2})/$1:/g'
>12:AS:D3:45:67:89:
>machine%
>--=20

>* http://universe.pc.helsinki.fi/~matti/contact.html *
>* +358-(0)40-593 50 91 but try first +358-(0)9-191 23978 *

#!/usr/bin/perl -w
$fmac=3D"123456789abc";
print join(":",($fmac =3D~/(\S\S)/g)),"\n";

works as well....

--------------

PGP fingerprint =3D  79 2F 58 0A EF 44 F9 72  14 27 C1 96 47 B4 E2 7F



Wed, 19 Apr 2000 03:00:00 GMT  
 
 [ 13 post ] 

 Relevant Pages 

1. Seeking advice on semantics (pros / cons and best ways of) using constants across modules

2. Better ways to deal with yppasswd?

3. Better ways to do this?

4. Any better ways of doing this ?

5. Looking for better ways of solving a pattern matching and replacement task...

6. EDITING DATAFILES - What is best of 3 ways?

7. fast ways to count substrings

8. Ways to test for membership?

9. Two ways to undef?

10. parse response from system query - too many ways to do it

11. ways to split big modules ?

12. ways around a ".asp"

 

 
Powered by phpBB® Forum Software