in reply to Re: Help me decipher code containing map function
in thread Help me decipher code containing map function

Browser, I very much prefer your method of unpacking the MAC. I have implemented it into my code. Thank you for taking the time to explain. Now I am doubting everything I learned in class :/. I have an additional question, can I compare a packed MAC just as I can compare it after unpacked? like:

if($packed1 eq $packed2){ print "The packed MACs match\n"; else{ print "The packed MACs do not match\n"; }

Replies are listed 'Best First'.
Re^3: Help me decipher code containing map function
by BrowserUk (Patriarch) on May 24, 2017 at 04:33 UTC
    can I compare a packed MAC just as I can compare it after unpacked?

    Yes. Before you unpack and format them, your octet strings are just sequences of bytes with binary values in the range 0 -> 255. Ie. strings. Perl's normal string comparison operations (eq, ne, lt, le, gt, ge) will work perfectly on them; and you can use cmp to sort them.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
    In the absence of evidence, opinion is indistinguishable from prejudice. Suck that fhit

      Excellent, that is the answer I was looking for. Thanks.

Re^3: Help me decipher code containing map function
by AnomalousMonk (Archbishop) on May 23, 2017 at 17:09 UTC

    I will take the liberty of answering for BrowserUk. I'm not sure what $packed1 and $packed2 are supposed to represent. If $packed1 is the original, raw MAC address and $packed2 is the reformatted address (with bytes represented as hex-digit pairs and with dots thrown in for good measure), then no, they cannot be directly compared:

    c:\@Work\Perl\monks>perl -wMstrict -le "my $octetstr = qq{\x44\x69\x66\x66\x65\x72}; print qq{raw: '$octetstr'}; ;; my $mac = join '.', unpack '(H4)*', $octetstr; print qq{reformatted: '$mac'}; ;; if ($octetstr eq $mac) { print qq{'$octetstr' and '$mac' are equal}; } else { print qq{'$octetstr' and '$mac' are NOT equal}; } " raw: 'Differ' reformatted: '4469.6666.6572' 'Differ' and '4469.6666.6572' are NOT equal
    (The two strings would have to be converted to a common format for comparison.)

    If $packed1 and $packed2 are both reformatted MAC addresses (and they've both been reformatted according to the same scheme), then yes, they can be directly compared for equality.

    Incidentally, this is another question that's open to resolution by direct experimentation. Could you not have written a couple of short routines to answer your own question — plus some variations?


    Give a man a fish:  <%-{-{-{-<