I believe that
BrowserUk and
AnomalousMonk clearly have the best code to use in place of your code from the class: Shorter, more clear, runs faster.
I would like to make a few comments about map. All maps can be written as "foreach" loops. Use "for" instead of "foreach" if you want - these keywords mean exactly the same.
The code that you started with uses what is called a "bare map", a map which does not make use of the potential output list. Most maps are like: @out_array = map{func..}@in_array; In your case, there is no @out_array and the map function works because of side-effects within the map's code. In this case $val is being modified.
I re-wrote the map code into a foreach loop as a demo. This allows you to add print statements to see what the loop is doing. And of course since this is a foreach loop, the $_ variable can be some other name (also see below). Performance between a map and a foreach version should be similar.
I personally only use map for short transformation operations, perhaps making a hash table from a list of values my %hash = map{$_ => 1}@list;. In this hash table case, the code is a "one liner" that makes use of the map's output, i.e. it is not a "bare" map. I don't use map's that do not make use of the left hand assignment. I have seen map functions with 20 lines of code. Again, I would recommend a foreach loop in that situation.
I hope the demo code below helps:
#!/usr/bin/perl
use strict;
use warnings;
my $octetstr = join '', map chr(), 0xab, 0xcd, 0x00, 0x1d, 0x94, 0x56;
my $val;
map { $val .= sprintf("%02x",$_) } unpack "CCCCCC", $octetstr;
my $mac=join(".",unpack("a4 a4 a4",$val));
print "MAC1: $mac\n";
########################
#### Exactly the same thing with foreach instead of map{}
########################
$val='';
foreach my $decimal_value (unpack "CCCCCC", $octetstr)
{
$val .= sprintf("%02x",$decimal_value); # makes 2 digit Hex
print "Decimal_value: $decimal_value \tval(hex string)=$val\n";
}
my $mac2=join(".",unpack("a4 a4 a4",$val));
print "MAC2: $mac2\n";
# the a4 unpack for $mac2 could be done in other, slower ways
# like with a regex (a very,very slow way, but just a hypothetical)
#
my $mac3 = $val;
$mac3 =~ s/^([0-9a-f]{4})([0-9a-f]{4})([0-9a-f]{4})/$1\.$2\.$3/;
print "MAC3: $mac3\n";
#######################
### BrowserUk, AnomalousMonk ideas with either H2 or H4
### far better!! One step, fast. Clearly the right way
### for this specific problem!
#######################
my $mac4 = join '.', unpack '(H4)*', $octetstr;
print "MAC4: $mac4\n";
__END__
All of these MAC's are the same:
MAC1: abcd.001d.9456
Decimal_value: 171 val(hex string)=ab
Decimal_value: 205 val(hex string)=abcd
Decimal_value: 0 val(hex string)=abcd00
Decimal_value: 29 val(hex string)=abcd001d
Decimal_value: 148 val(hex string)=abcd001d94
Decimal_value: 86 val(hex string)=abcd001d9456
MAC2: abcd.001d.9456
MAC3: abcd.001d.9456
MAC4: abcd.001d.9456
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.