in reply to Re^2: underline variable width column headings
in thread underline variable width column headings

The colored(...) function wants a character string argument, so you must call sprintf first to convert your numbers to strings, then colored. This fix works (at least on Linux):
push @line, colored(sprintf('%2d', scalar @{$trigraphs{$k}}), 'und +erline') . $k . colored(sprintf('%-2d',scalar keys %{$flanks{$k}}), 'underline') +;

Replies are listed 'Best First'.
Re^4: underline variable width column headings
by wlegrand (Initiate) on Dec 17, 2006 at 04:10 UTC
    Quester, thank you, your solution helped. I have tried everything I could think of to get a proper variable width underline with no success so I will have to settle for what I’ve got. I get 5 underlines under each heading, but at least the columns line up properly when I use:
    push @line, colored(sprintf('%2d', scalar @{$trigraphs{$k}}), 'underli +ne') . colored(sprintf('%s', $k), 'underline') . colored(sprintf('%-2 +d', scalar keys %{$flanks{$k}}), 'underline');
    What puzzles me is that It’s possible to get the variable width underline that I want when I use:
    push @line, sprintf('%2s%s%-2s', colored(scalar @{$trigraphs{$k}}, 'un +derline'), colored($k, 'underline'), colored(scalar keys %{$flanks{$k +}}, 'underline') );
    I just don’t know how to get it with the columns lining up.
      The columns aren't lining up because the character string returned by colored ($x, "underline") is longer than $x, typically by eight characters. That means that sprintf "%2s", colored(...) will never add an extra space.

      The easiest way that I can think of to handle this is to put in the extra spaces as an explicit extra step, like this:

      push @line, " " x ( 2 - length scalar @{ $trigraphs{$k} } ) . colored( scalar @{ $trigraphs{$k} }, 'underline' ) . colored( $k, 'underline' ) . colored( scalar keys %{ $flanks{$k} }, 'underline' ) . " " x ( 2 - length scalar keys %{ $flanks{$k} } );
        Quester, Perfect! Thank you for your code. Thank you for your explanations. Thank you for showing me an excellent way to break across lines, ... but most of all ... Thank you for going the extra mile in helping a novice.