LanX has asked for the wisdom of the Perl Monks concerning the following question:
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:
$ 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?
a bit shorter |perl -pe 'chomp, $_.=" "x(48-length$_) . " #:$1\n" if /(^#|nextstate|var\d+)/;'
meh, I expected a printf solution to be shorter
|perl -pe 'chomp, printf ("%-48s #:$1\n",$_), $_="" if /(^#|var|nextstate)/'
|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 | |
by LanX (Saint) on Nov 15, 2018 at 22:58 UTC | |
|
Re: One-liner to append text at fixed column
by Veltro (Hermit) on Nov 16, 2018 at 12:18 UTC | |
by LanX (Saint) on Nov 16, 2018 at 13:29 UTC | |
by Veltro (Hermit) on Nov 16, 2018 at 15:31 UTC | |
by LanX (Saint) on Nov 16, 2018 at 15:55 UTC |