I am trying to create a subroutine that will take in an array of MAC addresses and simply return a different format to match that of the device I'm telnetted into. For example, the MAC 001589F453CA needs to be changed to AP0015.89F4.53CA
I can get this to work fine with a static scalar, but run into problems when I try to pass an array to the subroutine. I'm not sure what everyone feels is a lot of code, so I'll post this code as this is my first post. I have done extensive rewrites to this over the last several days before asking anyone else for help (I've read about folks just dumping their problems and expecting someone else to find it). You'll see from some of my print statements I'm trying to track the values of my variables within my code to find out where it breaks. But at this point, I'm just getting garbage by the third iteration of my loop.
#!c:/Perl/bin/perl
my @mac_addrs = ("0015FAA3F03A", "0015FAA3F03B", "0015FAA3F03C");
# CONVERT THE MAC ADDRESS ############################################
+##############
sub convert () {
my @array = @_;
print "Inside convert: @array\n";
my @hold;
my $j = 0;
my $length = scalar(@array); #Number of records in the input array
print "Length of inner array: $length\n";
for ($j; $j<$length; $j++) { #This loop is setup so that its iteration
+s equal the number of elements in the input array
print "Value of j is: $j\n";
$element = lc($array[$j]); #Convert uppercase MAC to lowercase
print "Inside element: $element\n";
my $i = 0;
@hold = (); #Reset the temporary hold array used to create the $ou
+t variable
while ($i<length($element)) { #for each element of the array, brea
+k it up into the 3 groups of 4 letters each
push (@hold, substr($element, $i, 4));
print "While substring: " . substr($element, $i, 4) . "\n";
$i += 4;
print "Just finished substring: @hold\n";
}
my $out = "AP" . $hold[0] . "." . $hold[1] . "." . $hold[2]; #Store th
+e results to a variable to be added to output array
print "Out: $out\n";
push (@array, $out); #Add the newly converted MAC address to our array
shift (@array); #Remove the original MAC addresses one at a time as th
+ey're not needed outside the sub
}
return @array;
print @array;
}
# CONVERT THE MAC ADDRESS ############################################
+##############
Thanks,
Scott
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.