in reply to join string in 2D array

Consider the code below....
use warnings; use strict; use Data::Dumper qw(Dumper); my @clk = ('ux_prim_clk', 'ux_side_clk', 'ux_xtal_frm_refclk'); #my @clk_output = map [ split /_/, $_ ], @clk; # Just as a note: Performance is the same with explict loop. # Every map statement can be expressed as a foreach() loop. # This is the same your previous statement - nothing wrong with it # just demo'ing what the map actually does... my @clk_output; foreach my $clk_line (@clk) { push @clk_output, [split "_",$clk_line]; } print Dumper \@clk_output; my @clk_new; foreach my $row_ref (@clk_output) { push @clk_new, join ("_",@$row_ref); } print Dumper \@clk_new; __END__ $VAR1 = [ [ 'ux', 'prim', 'clk' ], [ 'ux', 'side', 'clk' ], [ 'ux', 'xtal', 'frm', 'refclk' ] ]; $VAR1 = 'ux_prim_clk'; $VAR2 = 'ux_side_clk'; $VAR3 = 'ux_xtal_frm_refclk';
Could also be coded...this does the same thing:
use warnings; use strict; use Data::Dumper qw(Dumper); my @clk = ('ux_prim_clk', 'ux_side_clk', 'ux_xtal_frm_refclk'); my @clk_output = map [ split /_/, $_ ], @clk; print Dumper \@clk_output; my @clk_new = map{my $line =join ("_", @$_); $line}@clk_output; print Dumper \@clk_new;

Replies are listed 'Best First'.
Re^2: join string in 2D array
by AnomalousMonk (Archbishop) on May 05, 2019 at 00:51 UTC
    Could also be coded...this does the same thing:
    ...
    my @clk_new = map{my $line =join ("_", @$_); $line}@clk_output;

    But isn't it also true that the statement
        my @clk_new = map{my $line =join ("_", @$_)}@clk_output;
    would do the same thing? In which case, there seems to be no point to assigning to a lexical within the map block, so we're back to
        my @clk_new = map{ join('_', @$_) }@clk_output;
    or
        my @clk_new = map join('_', @$_), @clk_output;

    Prior to the introduction of the  /r modifier for  s/// substitution in Perl version 5.14 (see Regexp Quote Like Operators in perlop), there was sometimes a need for a map statement like
        map { (my $r = $_) =~ s{ ... }{foo}xms;  $r; }
    to avoid changing the aliased referent of  $_ in a data flow, but it doesn't seem useful here. (I can't tell you how many times I've had to remember to do this when a bunch of unexpected '1' characters suddenly showed up in my output!)

    A similar situation might arise if one wanted to stick a debug print-point
        map { print "... $_ ...";  $_; }
    into the middle of a data flow.


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

      All completely valid points.

      However, assigning to a simple lexical scalar within a map block is dirt cheap both in terms of memory and execution speed. Perl is probably gonna do something like that internally anyway, it just won't have a "name". If assigning a lexical name makes the code more clear, then why not?

      In this thread, I think the OP has some confusion about maps and foreach loops. So I showed a couple of ways for each significant loop in the code.