Hi All,

I have a piece of code that was given to me in class about 4 years ago. I am now starting to use the code heavily. I can simply copy and paste the code into any script requiring it, but I would rather know step by step exactly what the code is doing so that I am better informed to adjust it to other situations. Please let me know if the code is omitting anything that might be considered as default

I am using SNMP to retrieve the ARP table of a Cisco switch. The response is a mac address that comes back packed, so I must unpack it to present it in a human friendly manner.

The end result ends up being something like: abcd.001d.9456. The piece of code is this:

my $val; map { $val .= sprintf("%02x",$_) } unpack "CCCCCC", $octetstr; $mac=join(".",unpack("a4 a4 a4",$val));

Now, before you tell me to go and do some reading, I have. I am coming to you because I still have some doubts.I.E: I thought that the map function takes in a list(perhaps an array), I do not see the above code having a list passed to it. Unless the packed mac address contained in $octetstr is the list. I am also not quite sure what sprintf("%02x",$_) is ll about. Why is this needed? For better readability of the code, how can I rename the $_ variable. Why do i get an error when I start my code like "map { my $val .=" instead of declaring $val separately. Thank you.


In reply to Help me decipher code containing map function by adamZ88

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.