Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (5)
As of 2024-04-25 14:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found