in reply to Re^3: perlform ^* variable field length
in thread perlform ^* variable field length

Hi, I'm having an array that has 6 cols/fields and a col/field can have either a single line or multiple lines. So, I can say a array looks like as :

('col1 : line1 col2 : line2', 'col2 : line1', 'col3 : line1', 'col4 : line1', 'col5:line1 col5:line2', 'coll6 : line1 coll6 : line2 coll6 : line3 coll6 : line4' );

and I want to print the content of array as :

col1:line1|col2:line1|col3:line1|col4:line1|col5:line1|col6: line1 col2:line2||||col5:line2|col6:line2 |||||col6:line3 |||||col6:line4

Replies are listed 'Best First'.
Re^5: perlform ^* variable field length
by Not_a_Number (Prior) on Apr 28, 2015 at 08:01 UTC

    This seems to be exactly what you posted in Re^2: perlform ^* variable field length. You were asked to clarify, not repeat yourself.

    What I don't understand, for example, is why the second line in your "desired result" should be:

    col2:line2||||col5:line2|col6:line2

    rather than:

    |col2:line2|||col5:line2|col6:line2

      yes, you are correct, there was typo in that reply

      here's the corrected array

      ('col1 : line1 col1 : line2', 'col2 : line1', 'col3 : line1', 'col4 : line1', 'col5:line1 col5:line2', 'coll6 : line1 coll6 : line2 coll6 : line3 coll6 : line4' );

      and corrected desired result

      col1:line1|col2:line1|col3:line1|col4:line1|col5:line1|col6: line1 col1:line2||||col5:line2|col6:line2 |||||col6:line3 |||||col6:line4

      I think, you got how I want to print the content of array. Going by the perlform doc and using ^* alongwith ~~ the format string I'm using should work, but it throws Out of Memory Error!.

      or is there any other way i can get the desired result.

Re^5: perlform ^* variable field length
by pme (Monsignor) on Apr 28, 2015 at 08:37 UTC
    It seems to be that this is your input:
    my @row = ( 'col1:line1 col1:line2', 'col2:line1', 'col3:line1', 'col4:line1', 'col5:line1 col5:line2', 'col6:line1 col6:line2 col6:line3 col6:line4' );
    and this is the required output:
    col1:line1|col2:line1|col3:line1|col4:line1|col5:line1|col6:line1 col2:line2| | | |col5:line2|col6:line2 | | | | |col6:line3 | | | | |col6:line4
    Is that correct?

    Please read you comment carefully before posting.

      yes, its correct. just one correction in the desired output which you have posted in row 2

      col1:line1|col2:line1|col3:line1|col4:line1|col5:line1|col6:line1 ->col1:line2| | | |col5:line2|col6:line2 | | | | |col6:line3 | | | | |col6:line4
        This piece of code does what you need except that the field width is fixed.
        use warnings; use strict; my @row = ( 'col1:line1 col1:line2', 'col2:line1', 'col3:line1', 'col4:line1', 'col5:line1 col5:line2', 'col6:line1 col6:line2 col6:line3 col6:line4' ); format STDOUT = ^>>>>>>>>|^>>>>>>>>|^>>>>>>>>|^>>>>>>>>|^>>>>>>>>|^>>>>>>>>|~~ @row . write;
        However using variable field width gives "Out of memory", I do not know why.
        format STDOUT = ^*|^*|^*|^*|^*|^*|~~ @row .