Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Splitting multiline scalars into different array entries

by davis (Vicar)
on Aug 05, 2005 at 10:13 UTC ( #481165=perlquestion: print w/ replies, xml ) Need Help??
davis has asked for the wisdom of the Perl Monks concerning the following question:

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.

Comment on Splitting multiline scalars into different array entries
Download Code
Re: Splitting multiline scalars into different array entries
by Tanalis (Curate) on Aug 05, 2005 at 10:36 UTC
    I'm not 100% sure what you need, but assuming you need what I think you need .. :) .., replacing your loop with this works:
    foreach my $row (@AoA) { my @foo = map { split /\n/ } @$row; for( my $i = 0; $i < $#foo; $i += $width ) { push @new_rows, [ @foo[ $i .. -1 + $i + $width ] ]; } }
    This outputs:

    Any use to you?
      Sorry, should have provided wanted output. That's what I ended up with on my first try :-) wanted output: Cheers anyway!

      davis
      Kids, you tried your hardest, and you failed miserably. The lesson is: Never try.
        So you need to transpose - this thread should help.

        Flavio
        perl -ple'$_=reverse' <<<ti.xittelop@oivalf

        Don't fool yourself.
        I'm certain that this isn't the best way, but it works:
        foreach my $row (@AoA) { my @foo = break_up( $row ); if( $foo[0] =~ /ARRAY/ ) { push @new_rows, @foo; } else { push @new_rows, [ @foo ]; } } print Dumper(\@new_rows); sub break_up { my $row = shift; my $uses_newlines = 0; $uses_newlines++ if join( "", @$row ) =~ /\n/m; return @$row unless $uses_newlines; my @tmp; map { push @tmp, [ split /\n/ ] } @$row; my @row; for( my $i = 0; $i <= $#tmp; $i++ ) { for( my $j = 0; $j <= $#tmp; $j++ ) { $row[$i][$j] = shift @{$tmp[$j]}; } } return @row; }
        giving an output of

        Interesting problem :) What's this for, if I can ask?

Re: Splitting multiline scalars into different array entries
by broquaint (Abbot) on Aug 05, 2005 at 11:12 UTC
    use Data::Dumper; ## your @AoA here ... my @results = map { grep(/\n/, @$_) ? map([split "\n"], @$_) : $_ } @AoA; print Dumper( \@results );
    If you want to update the original data just change the my @results = to @AoA =. Also you could use Scalar::Util::first there instead of grep if you're looking for an optimal solution.
    HTH

    _________
    broquaint

      Hmmm... that was my first result. See my desired output. Thanks anyway!

      davis
      Kids, you tried your hardest, and you failed miserably. The lesson is: Never try.
        Ah yes, it seems I missed the transposition.
        use Algorithm::Loops 'MapCar'; my @results = map { grep(/\n/,@$_) ? MapCar {[@_]} map([split "\n"], @$_) : $_ } @AoA;
        And to be sure:
        HTH

        _________
        broquaint

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://481165]
Approved by Tanalis
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (11)
As of 2014-08-22 18:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (163 votes), past polls