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


In reply to One-liner to append text at fixed column by LanX

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.