I found myself grovelling through some fixed-width data and I realised that what I needed was one of those funky ASCII rulers to help me count column widths, those things that look like:
1 2 3 4 5 012345678901234567890123456789012345678901234567890
I whipped up something quickly and it does the job, but it's pretty ugly and I'm sure someone can do better either more elegantly and/or in a more compact manner.
The constraints are simple: a sub takes a scalar arg representing the length of the ruler. This number is rounded up to the next multiple of 10. Return the string representing the ruler.
When you get to the hundreds (and I did need a ruler that was 240 wide) it should look like this:
11 12 13 14 15 ...478901234567890123456789012345678901234567890
Golf, anyone?
sub ruler { my($n,$r) = ($_[0], ' '); $r .= ' 'x(10-length).$_ for (1..($n+9)/10); return "$r\n" . '0123456789' x (($n+9)/10) . "0\n"; }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: ASCII Rulers
by jmcnamara (Monsignor) on Apr 03, 2001 at 15:01 UTC | |
by grinder (Bishop) on Apr 03, 2001 at 15:57 UTC | |
Re: ASCII Rulers
by petral (Curate) on Apr 03, 2001 at 15:47 UTC | |
Re: ASCII Rulers
by chipmunk (Parson) on Apr 03, 2001 at 21:31 UTC | |
Re: ASCII Rulers
by jmcnamara (Monsignor) on Apr 03, 2001 at 17:05 UTC | |
Re: ASCII Rulers
by MrNobo1024 (Hermit) on Apr 03, 2001 at 19:43 UTC | |
Re: ASCII Rulers
by Tyke (Pilgrim) on Apr 03, 2001 at 17:02 UTC |
Back to
Cool Uses for Perl