LanX has asked for the wisdom of the Perl Monks concerning the following question:

Hi

I wanted to be able to mark lines with comments at a fixed column an came up with this one-liner solution.

Did I miss a more elegant solution?

|perl -pe 'substr $_, -1, 0, " "x(68-length$_) . "  #:$1" if /(^#|nextstate|var\d+)/;'

Explanation:

example with column 50

$ perl -MO=Concise,-src tst_b_xref4.pl|perl -pe 'substr $_, -1, 0, " " +x(48-length$_) . " #:$1" if /(^#|nextstate|var\d+)/;' tst_b_xref4.pl syntax OK j <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 # 1: for ( #:# 2 <;> nextstate(main 3 tst_b_xref4.pl:1) v:{ ->3 #:nextstate 4 <1> preinc[t2] vK/1 ->5 - <1> ex-rv2sv sKRM/1 ->4 3 <#> gvsv[*var1] s ->4 #:var1

Use case: Re^12: B::Xref buggy?

update

a bit shorter |perl -pe 'chomp, $_.=" "x(48-length$_) . "  #:$1\n" if /(^#|nextstate|var\d+)/;'

update

meh, I expected a printf solution to be shorter

|perl -pe 'chomp, printf ("%-48s  #:$1\n",$_), $_="" if /(^#|var|nextstate)/'

update

|perl -pe 'chomp, $_ = sprintf "%-48s  #:$1\n",$_ if /(^#|var|nextstate)/'

IMHO best solution so far. :)

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Replies are listed 'Best First'.
Re: One-liner to append text at fixed column
by jwkrahn (Abbot) on Nov 15, 2018 at 22:51 UTC
    perl -lne 'printf "%-48s%s\n", $_, /(^#|nextstate|var\d+)/ ? " #:$1" +: ""'
      ah yes -l for chomp. Thanks! :)

      |perl -lpe '$_= sprintf "%-48s  #:$1",$_ if /(^#|nextstate|var\d+)/'

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Re: One-liner to append text at fixed column
by Veltro (Hermit) on Nov 16, 2018 at 12:18 UTC

    Fiddling around a bit, trying to find a solution where it becomes more clear what is being searched for and which characters are being replaced. But still, it is not very pretty.

    $_ = "abc_something" ; print $_ . "\n" if /abc/ && substr( $_, 0, 4 ) =~ s/(.+)/" "x((length $1)+2)."#:"/e ;

    The substr just takes care of replacing the first part with "      #:". Make sure that the search for 'abc' is done before doing the replacement since it is greedy.

      If it comes to readability I prefer my last sprintf solution with the twist of changing if to and .

      perl -lpe " /(match1|match2)/ and $_ = sprintf qq{%-68s  #$1}, $_ "

      Normally I don't like short-circuit expressions, but in this case it really helps clarifying what's happening.

      Demo: (win quoting)

      >perl -E"say '.' x rand(80) for 1..10" |perl -lpe " /(.)/ and $_ = spr +intf qq{%-68s #$1},$_ " ...................................................................... +...... #. ...................... +#. ..................... +#. ...................................................................... +.... #. ......................................................... +#. ...................................... +#. ......................... +#. ........................................................ +#. ...................................................................... +... #. ......................... +#.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

        Ok, I think I did not understand the original problem. I thought you were trying to 'align' all the comments from a text like this:

        perl -E"say '.' x rand(80) . '# test comment' for 1..3" ......................# test comment .................................# test comment ...................................................................... +....# test comment

        So eventually I came up with this which I think is too complex.

        perl -E"say '.' x rand(80) . '# test comment' for 1..10" |perl -lpe " +/\s*#(.*)$|$/ && ( substr( $_, $-[0], length($_) ) = qq{ }x(68 - $-[ +0] + 2) . qq{ #$1} ) ............................................... + # test comment .......................................................... + # test comment ....................................... + # test comment ............................................................ + # test comment ...................................................................... +.... # test comment ...... + # test comment ........................................ + # test comment ............................... + # test comment ................................................... + # test comment ................................... + # test comment

        Maybe it can be rewritten with a printf but I will leave it with this.