Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re: One for the regexp fans

by davorg (Chancellor)
on Aug 04, 2000 at 13:37 UTC ( #26156=note: print w/replies, xml ) Need Help??

in reply to One for the regexp fans

Do MAC addresses always have six sections? If so, you could do something like this:

my $fmt = '%02x' x 6; $mac = sprintf $fmt, map hex, split /\./, $mac;

It's still two lines, but the first is just there so I don't have to type '%02x' six times :)

Oh, and it's not really a regex solution - sorry!


European Perl Conference - Sept 22/24 2000, ICA, London

Replies are listed 'Best First'.
RE: Re: One for the regexp fans
by merlyn (Sage) on Aug 05, 2000 at 10:06 UTC
    davorg says:
    It's still two lines, but the first is just there so I don't have to type '%02x' six times :)
    Well then, don't!
    $mac = sprintf "%02x" x 6, map hex, split /\./, $mac;
    There. One line. :)

    -- Randal L. Schwartz, Perl hacker

RE: (2) One for the regexp fans (nnnn:nnnn:nnnn)
by ybiC (Prior) on Aug 04, 2000 at 15:05 UTC
    Another common format for MAC address display is nnnn:nnnn:nnnn (common amongst Cisco gear, anyway).   In either case, it's the same 12 hex digits, only the delimiter and break-points change.

    Is simple code that handles both formats possible?

      You could probably do something like:
      if $str =~ m!\.! {$str =~ s/(\.?[0-9a-eA-E]*)/(substr("0$1", -2))/ge;} else {$str =~ s/(:?[0-9a-eA-E]*)/(substr("0$1", -4))/ge;}


RE: Re: One for the regexp fans
by Odud (Pilgrim) on Aug 04, 2000 at 13:43 UTC
    Nice idea - I'd got a bit hung up on the solution being an RE. I think that they are always "dotted-sex!" format (the string comes from calling netstat -i and I have to cope with the different formats produced by HP-UX, AIX, and OSF1). Perhaps we can have a pint together at yapc::Europe?
      Not much to offer except curiosity and frustration on my own efforts. On your HP-UX interface queries, does it render "raw" (no colon or dash delimiter) 12 characters, with leading zeroes, or does it provide you with the delimiter. I'm using "lanscan -a" on the HP-UX interfaces, getting my 12 char MAC address, trying to insert colons every two characters, then stripping the lead zeroes. That's my fallback position after I baked my brain on making the DEC OSF1 "netstat -i | grep '<Link>' |egrep -v "s10|lo0|ppp0"|awk '{print $1}' | sort -u`" system call for-loop kludge work, which at least renders multi-line output of valid, live interfaces. If I can get either to work, I'll die a happy I thrust my head through the display :-) ...---... SOS !!! -raddude
        In the end I decided that I didn't need the :s - and so I just take whatever format the OS (HP-UX, AIX, or OSF/1) reports and use that. The justification being that if you need to get involved with MAC addresses then you can probably cope with the different formats!

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2021-02-24 21:22 GMT
Find Nodes?
    Voting Booth?

    No recent polls found