Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: join string in 2D array

by roboticus (Chancellor)
on May 03, 2019 at 12:13 UTC ( [id://1233315]=note: print w/replies, xml ) Need Help??


in reply to join string in 2D array

Newbie95:

The answer Anonymous Monk said was very good. As stated, you were pretty close *and* a C-style for loop often indicates potential for improvement. Going from the first paragraph to the second, though, has a couple missing steps that may not be obvious, so I'm replying only to fill in a couple of those skipped steps.

Making the indicated change, and tweaking the call to Dumper gives you:

for ( my $c= 0; $c<= $#clk_output; $c++) { for ( my $d = 0; $d <= $#{$clk_output[$c]}; $d++ ) { $clk_new = join '_', @{$clk_output[$c]}; push @clk_new, $clk_new; } } print Dumper \@clk_new;

which gives you (edited for space):

$VAR1 = [ 'ux_prim_clk', 'ux_prim_clk', 'ux_prim_clk', 'ux_side_clk', 'ux_side_clk', 'ux_side_clk', 'ux_xtal_frm_refclk', 'ux_xtal_frm_refclk', 'ux_xtal_frm_refclk', 'ux_xtal_frm_refclk' ];

Now you have the result with the problem that there are too many copies of each value. Since join accepts a list, you didn't need the inner loop at all. Removing it give you:

for ( my $c= 0; $c<= $#clk_output; $c++) { $clk_new = join '_', @{$clk_output[$c]}; push @clk_new, $clk_new; } print Dumper \@clk_new;

which gives the desired result:

$VAR1 = [ 'ux_prim_clk', 'ux_side_clk', 'ux_xtal_frm_refclk' ];

But as the Anonymous Monk mentions, a C-style loop frequently gives you the chance for improvements. Rather than looping over the array indices and using the index to fetch the value to operate on, you can loop over the array values using them directly:

for my $c (@clk_output) { $clk_new = join '_', @$c; push @clk_new, $clk_new; }

Next, you're creating $clk_new only to use it in the next statement and then throwing it away. So the next improvement is to skip creation of the temporary variable $clk_new, like this:

for my $c (@clk_output) { push @clk_new, join '_', @$c; }

In fact, now you're using another variable that you're using only once: $c. If you leave out "my $c" from the for loop, perl will use the $_ variable to hold each value, letting you say:

for (@clk_output) { push @clk_new, join '_', @$_; }

Then we come to very common operation: building a new list by performing an operation on each element of another list. It's such a common operation that perl offers (as many languages do) a way to do that a little more directly, the map operator, which gives you the ability to create a new list given a list and a block of code to operate on each element:

@new_list = map { operation_to_perform_on_each_element } @old_list;

Like before, inside the block the $_ variable contains the current element from the original list. Making that change gives you the final code that Anonymous Monk gave you:

@clk_new = map { join '_', @$_ } @clk_output;

Edit: A couple grammatical things (missing words) and an fix to one of the outputs.

...roboticus

When your only tool is a hammer, all problems look like your thumb.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1233315]
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: (2)
As of 2024-04-26 06:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found