Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Hi,

I'm trying to split an AoA into different entries if individual scalar values are multiline values. (I thought this was called "pivoting", but looking at Data::Pivot suggests I'm wrong). Super Searching on "split multiline array" doesn't seem to reveal much.

What I've tried already doesn't work as intended (it should be pretty obvious what end result I'm looking for) and I don't think I need to know the width of the arrays.

Any help gratefully received. Does anyone know what the technique should be called for a start?

use warnings; use strict; use Data::Dumper; my @AoA = ( [qw/single cell values/], [qw/are really easy/], ["but multiline stilton", "these cells is", "aren't suck great"], [qw/back to life/], [qw/back to reality with more cells/]); #This is where the garbage begins... my @new_rows; my $width = 3; foreach my $row (@AoA) { my @cells = @$row; my @foo = map { split /\n/ } @cells; my $new_row = 0; my $new_row_idx = 0; for(my $idx = 0; $idx<$#foo; $idx++) { if(($idx % $width) == 0) { $new_row++; $new_row_idx = 0; } $new_rows[$new_row][$new_row_idx] = $foo[$idx]; $new_row_idx++; } } print Dumper(\@new_rows);
Cheers
Update: Forgot to say: here I'm only printing out the attempted newly-created array. I'd prefer to re-create the original array with the split data, with the rows in the correct order

davis
Kids, you tried your hardest, and you failed miserably. The lesson is: Never try.

In reply to Splitting multiline scalars into different array entries by davis

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, 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, 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.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others studying the Monastery: (4)
    As of 2020-05-27 10:14 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      If programming languages were movie genres, Perl would be:















      Results (154 votes). Check out past polls.

      Notices?