http://qs1969.pair.com?node_id=1190703


in reply to Help me decipher code containing map function

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

Replies are listed 'Best First'.
Re^2: Help me decipher code containing map function
by shmem (Chancellor) on Jun 11, 2017 at 12:17 UTC
    map { $val .= sprintf("%02x",$_) } unpack "CCCCCC", $octetstr;

    Nice example for map in void context. I wouldn't transform that into a full foreach loop, but use a statement modifier instead

    $val .= sprintf("%02x",$_) for unpack "CCCCCC", $octetstr;

    for the same reasons I wrote down elsewhere.

    But then - regarding map in void context - all perl statements are executed for their side effect, and the computed value is discarded:

    # result from map is discarded # side effect: $val is populated map { $val .= sprintf("%02x",$_) } unpack "CCCCCC", $octetstr; # result from print is discarded (1 if successful) # side effect: text appears on STDOUT or selected filehandle print "hello, world!\n"; # result from assignment is 42, again discarded # side effect: $value is populated $value = 42; # result from die is discarded (if any), print not done # side effect: well... print die;
    perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
Re^2: Help me decipher code containing map function
by adamZ88 (Beadle) on May 24, 2017 at 14:28 UTC

    Your Sample code certainly helps! Thank you!

A reply falls below the community's threshold of quality. You may see it by logging in.