Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Align given substring(s) in lines

by johnaj (Sexton)
on Apr 28, 2021 at 18:55 UTC ( [id://11131810]=CUFP: print w/replies, xml ) Need Help??

I chose not to use strict for this one, and it was actually kind of nice for a simple script like this one. Especially the autovivification. Note that the script doesn't work too well if the alignment points you specify don't appear in the same order as you specify them. The script could also be optimized by getting rid of the separate first pass.

#!/usr/bin/perl -w # align -- align given substrings in standard input lines use Getopt::Std; $Getopt::Std::STANDARD_HELP_VERSION = 1; getopts('a', \%opt); sub VERSION_MESSAGE {} sub HELP_MESSAGE { print <<OPT; usage: $0 [-s] [string ...] -a add an extra space before split You should put the strings in the order that they occur in each line. Note that only the last string is guaranteed to align. OPT } @splits = @ARGV; # alignment points @splits = (' #') if not @splits; # default (Perl comment) while (<STDIN>) { chomp; push @lines, $_; } for $split (@splits) { $max = 0; # Find max column for split string for (@lines) { if (/(.+?)(\Q$split\E)(.*)/) { $max = length $1 if length $1 > $max; $_ = [$1, "$2$3"]; } } # Add one extra space before split $max++ if $opt{a}; # Add spaces before split for (@lines) { $_ = sprintf("%-${max}s%s", $_->[0], $_->[1]) if ref; } } print "$_\n" for @lines;

Here is an example of how to use the script from the UNIX shell:

$ cat example my $string = 'some value'; # these my $int = 12; # are my $float = 1.2; # scalars $ cat example | ./align = \# my $string = 'some value'; # these my $int = 12; # are my $float = 1.2; # scalars

I personally use it to great effect in vi, so that I can type !}align to align the comments in the next block of code.

Replies are listed 'Best First'.
Re: Align given substring(s) in lines
by haukex (Archbishop) on Apr 28, 2021 at 19:27 UTC

    As to your example, note perltidy does this too, though of course it only works on Perl and does a whole lot more as well.

    $ cat example.pl my $string = 'some value'; # these my $int = 12; # are my $float = 1.2; # scalars $ perltidy -b example.pl $ cat example.pl my $string = 'some value'; # these my $int = 12; # are my $float = 1.2; # scalars
      That's neat -- I've never used perltidy, as I'm not a really big fan of automated/mandated coding style (unless I'm reading obfuscated code), but that's a pretty good use case.
        I'm not a really big fan of automated/mandated coding style (unless I'm reading obfuscated code)

        When I'm writing code, I just write it in my own style anyway, and when working on someone else's code that has a consistent style I usually try to copy that. I basically only use perltidy when someone else's code is hard to read (like some of the questions posted here sometimes have mixed up indentation), and I've worked out my own perltidyrc that produces output that's close enough to my own style. Running perltidy on my code would usually mess up my style in a few small places so I don't do that :-)

Re: Align given substring(s) in lines
by Fletch (Bishop) on Apr 28, 2021 at 22:14 UTC

    Similar to the perltidy recommendation: Emacs has some things like M-x align-regexp which will line up things matching a regular expression vertically.

    Edit: Or if you're a heathen: vim Align plugin

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: Align given substring(s) in lines
by GrandFather (Saint) on Apr 28, 2021 at 21:58 UTC

    I don't understand your reason for not using strict.

    Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond

      Without strict you can explore and try things more quickly and easily. If you use strict, then you have to write quite a bit of boilerplate, like variable declarations, which you are most likely going to have to remove later anyway, as you work your way through the problem and alter your approach.

      Basically, without strict you can get a working prototype much faster. Then you can add strict and all the usual boilerplate when you're done.

      Though I must admit I like that poem... :-)

        That goes completely against my experience (15+ years with Perl). The only "boilerplate" required by strict is to declare variables which typically consists of three extra characters - that is not onerous.

        Best practice is to only declare variables in the scope they are used and to initialize them when they are declared. In your sample code that amounts to 28 extra characters counting the "use strict;" statements. That is about a 2% overhead. 2% does not equate to "much faster" in my book.

        "running prototype" != "working prototype". A simple typo in a variable name that strict would have alerted you to can easily soak up tens of minutes of debugging time or introduce subtle calculation or logic errors that may go unnoticed. Just one case like that detected by strictures will more than outweigh the trivial overhead of the "boilerplate" over tens of projects.

        Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://11131810]
Approved by Discipulus
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-03-28 14:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found