#!/usr/bin/perl -w # Apply gravity to all uppercase characters such that they fall on top # of non-whitespace characters. # # 2018-01-15 use strict; my @lines = <>; my $width = 0; foreach (@lines) { s/\s*$//s; $width = length($_) if $width < length($_); } foreach (@lines) { $_ .= ' ' x ($width - length($_)); } for(my $y = $#lines - 1; $y >= 0; $y--) { for(my $x = 0; $x < $width; $x++) { my $c = substr($lines[$y], $x, 1); next unless $c =~ /[A-Z]/; for(my $d = $y; $d < $#lines; $d++) { last if substr($lines[$d + 1], $x, 1) ne ' '; substr($lines[$d], $x, 1) = ' '; substr($lines[$d + 1], $x, 1) = $c; } } } print "$_\n" foreach @lines;