I always look at these golf challenges and think "I bet x would be shorter" but it rarely is. Anyway:
sub ruler {
my($n,$r) = (int($_[0]+9)/10, ' ');
$r .= sprintf"%10s",$_ for (1..$n);
"$r\n" . '0123456789' x $n . "0\n";
}
The sprintf would save a few strokes in your code but nothing significant.
I'll take your word for it that there is a column 0. ;-)
For shorter rulers I often use a sequence like this which fits on one line and is easy to count::
123456789_123456789_123456789_123456789_123456789_123456789_
John.
--
| [reply] [d/l] [select] |
| [reply] [d/l] |
A couple of minor golfish things, without really changing the algorithm:
sub ruler {
my $n = 9 + shift; chop $n;
join "", " ", map(" "x(9-/../).$_, 1..$n), "\n",
"0123456789" x $n, "0\n"
}
But for filling exactly to (arg) columns (eg, to avoid wrapping when using $ENV{COLUMNS}):
sub ruler {
my $n = shift;
join"", " ", map(" "x(9-/../).$_, 1..$n/10), "\n",
map(/.$/g, 0..$n), "\n"
}
update: or (of course) ye olde one-liner:perl -lwe 'print" ",map(" "x(9-/../).$_,1..$ARGV[0]/10),"\n",map/.$/g,0..shift'
further update: chipmunk got it.  He's almost using printf as a map!
p | [reply] [d/l] [select] |
61 characters in the body of this subroutine:
sub ruler {
printf$".'%10s'x($l=(9+pop)/10)."$/0".1234567890x$l.$/,1..$l;
}
| [reply] [d/l] |
sub ruler {
my$r=($_[0]+9)/10;" @{[map{sprintf'%9s'
,$_}1..$r]}\n0".1234567890x$r."\n"
}
| [reply] [d/l] |
A second attempt (some whitespace remains):
sub ruler {
my $n = ($_[0]+9)/10;
(sprintf" "."%10s"x$n."\n",1..$n).'0123456789'x$n."0\n";
}
As a one liner (75 bytes):
perl -e'$n=(9+shift)/10;printf" "."%10s"x$n."\n%s0\n",1..$n,"012345678
+9"x$n'
John.
--
| [reply] [d/l] [select] |
This is another approach that might not be shorter, but
does work with ruler lengths that aren't a multiple of
10. Update: Just noticed that petral does it
more neatly.
#!perl -w
use strict;
print ruler(@ARGV);
sub ruler {
my ($x,$y) = (' ', '');
map{$x.=sprintf('%10d',$_/10) if $_%10 == 0 && $_ != 0; $y.=$_%10} (
+0..$_[0]);
return "$x\n$y\n";
}
I would have liked to use for rather than
map, but I couldn't get the 1 line version
to compile ;-(
I also can't get it to work with non-positive integer
values ;-((. | [reply] [d/l] [select] |