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.


In reply to Re: join string in 2D array by roboticus
in thread join string in 2D array by Newbie95

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.