Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Splitting multiline scalars into different array entries

by Tanalis (Curate)
on Aug 05, 2005 at 10:36 UTC ( #481171=note: print w/ replies, xml ) Need Help??


in reply to Splitting multiline scalars into different array entries

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:

$VAR1 = [ [ 'single', 'cell', 'values' ], [ 'are', 'really', 'easy' ], [ 'but', 'multiline', 'stilton' ], [ 'these', 'cells', 'is' ], [ 'aren\'t', 'suck', 'great' ], [ 'back', 'to', 'life' ], [ 'back', 'to', 'reality' ], [ 'with', 'more', 'cells' ] ];

Any use to you?


Comment on Re: Splitting multiline scalars into different array entries
Select or Download Code
Replies are listed 'Best First'.
Re^2: Splitting multiline scalars into different array entries
by davis (Vicar) on Aug 05, 2005 at 10:40 UTC
    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?

        Yep, that certainly works! Cheers! I'm going with broquaint's solution however.
        Interesting problem :) What's this for, if I can ask?
        Yeah, sure. I've got to process some (well, lots of) PDF files. I'm converting them to .xls (Excel) format with PDFConverter, then processing the file with Spreadsheet::ParseExcel. Even though PDFConverter's pretty good, it doesn't always cope with some tables, leaving me a mess similar to my example data.

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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2015-08-01 00:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (285 votes), past polls