in reply to Avoid warnings while formatting with repeated format lines

G'day hexcoder,

Here's a generic solution to your issue.

#!/usr/bin/env perl use strict; use warnings; my $multi_data_1 = [qw{ 123 456 789 0 _-=+ qwe rty uio p {[]} asd fgh jkl :; '" zxc vbn m <>?/ }]; print_table(MULTI_COL => 'QWERTY Keyboard', $multi_data_1, 5); my $step_data = [qw{Step2 0 4 Step3 4 4}]; print_table(STEP => 'TAB', $step_data, 3); my $multi_data_2 = [ 'skip1', '', 'skip2', '', '', 'skip2', '', '', 'skip1', '', 'skip', 'rest', ]; print_table(MULTI_COL => 'Skipped Cells', $multi_data_2, 5); sub print_table { my ($form, $title, $all_data, $cols) = @_; format STEP = .===================. |@<<<<<<<<<<<<<<<<< | $title .=======.===========. |@<<<<<<| @<</@>> |~~ get_col_data($all_data, $cols) .=======.===========. . format MULTI_COL = /=======================================\ | @|||||||||||||||||||||||||||||||||||| | $title |---------------------------------------| | @<<<< | @<<<< | @<<<< | @<<<< | @<<<< |~~ get_col_data($all_data, $cols) \=======================================/ . local $~ = $form; write; return; } sub get_col_data { my ($all_data, $cols) = @_; my @cols_data = splice @$all_data, 0, $cols; return @cols_data, ('') x ($cols - @cols_data); }

Output:

/=======================================\ | QWERTY Keyboard | |---------------------------------------| | 123 | 456 | 789 | 0 | _-=+ | | qwe | rty | uio | p | {[]} | | asd | fgh | jkl | :; | '" | | zxc | vbn | m | <>?/ | | \=======================================/ .===================. |TAB | .=======.===========. |Step2 | 0 / 4 | |Step3 | 4 / 4 | .=======.===========. /=======================================\ | Skipped Cells | |---------------------------------------| | skip1 | | skip2 | | | | skip2 | | | skip1 | | | skip | rest | | | | \=======================================/

Update: In get_col_data(), I changed three instances of @step_data to @cols_data. The output is unchanged. The original @step_data was from when I was initially testing with the OP's format (with Step2 & Step3). The change is in line with my opening statement that this was a "generic solution".

— Ken

Replies are listed 'Best First'.
Re^2: Avoid warnings while formatting with repeated format lines
by hexcoder (Curate) on Aug 24, 2022 at 17:48 UTC
    Thanks kcott!

    That is appreciated a lot! I will definitively bookmark this solution for later reference.

    What would also be really cool, is using format also dynamically in the horizontal direction. That would probably mean to use eval and hook into the lowlevel formatting, I guess.

    hexcoder
      What would also be really cool, is using format also dynamically in the horizontal direction.

      I'll just mention that Text::Table exists so that you (and I) don't have to wrestle with such things. OTOH, if you were intent on a challenge ...


      🦛

        There is also Perl6::Form, Damian Conway's implementation of the interface that was designed for Perl6 (now Raku) to replace Perl5's concept of formats.

      "What would also be really cool, is using format also dynamically in the horizontal direction. That would probably mean to use eval and hook into the lowlevel formatting, I guess."

      You would need eval but shouldn't need to mess around with *STEP{FORMAT} or anything like that. There's an example in "perlform: NOTES"; search for "The truly desperate can ...". :-)

      I used to use format quite regularly 20-25 years ago (probably a couple of times a week for generating all manner of reports). Although fairly proficient back then, I often encountered pulling-my-hair-out-in-frustration situations. These days, I'd generally look to other solutions; I don't think I've used it this millenium.

      — Ken