Skip to content

Instantly share code, notes, and snippets.

@mmcclimon
Last active June 22, 2021 00:50
Show Gist options
  • Save mmcclimon/276f3c74fa0657991046ce0b5c19d523 to your computer and use it in GitHub Desktop.
Save mmcclimon/276f3c74fa0657991046ce0b5c19d523 to your computer and use it in GitHub Desktop.
robcol.pl
#!/usr/bin/env perl
use v5.26;
use warnings;
use Getopt::Long::Descriptive;
my ($opt, $usage) = describe_options(
'%c %o',
[ 'padding|p=i', 'spaces to separate columns by', { default => 2} ],
[ 't', 'this does nothing but helps your muscle memory' ],
[ 'help|h', 'print usage and exit', { shortcircuit => 1 } ],
);
print($usage->text), exit if $opt->help;
my @lines;
my @field_maxes;
# Dump lines into an array, keeping track of the max field lengths as we go.
while (my $line = <<>>) {
chomp $line;
push @lines, $line;
# skip comments
next if $line =~ /^#/;
my @fields = split /\s+/, $line;
for my $i (0..$#fields) {
my $max = $field_maxes[$i] // 0;
my $len = length $fields[$i];
$field_maxes[$i]= $len > $max ? $len : $max;
}
}
# now we know how long to make the columns; just do that.
my $space = q{ } x $opt->padding;
my $format = join $space, map { "\%-${_}s"} @field_maxes;
for my $line (@lines) {
if ($line =~ /^#/) {
say $line;
next;
}
no warnings 'missing'; # shut up perl, you don't know me
my @fields = split /\s+/, $line;
printf "$format\n", @fields;
}
__END__
Handcrafted, artisanal perl, direct from South Philadelphia.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment