Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^2: join string in 2D array

by AnomalousMonk (Archbishop)
on May 05, 2019 at 00:51 UTC ( [id://1233369]=note: print w/replies, xml ) Need Help??


in reply to Re: join string in 2D array
in thread join string in 2D array

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:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^3: join string in 2D array
by Marshall (Canon) on May 05, 2019 at 03:48 UTC
    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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1233369]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-04-23 23:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found