Conditions state we assume the # of rows is in $a, so not
counting the initial assignment, 41 characters:
$a=5;
print map{@_=$j+1..($j+=++$i);"@_\n"}1..$a
#Update 2 chars better (39)
print map{"@{[$j+1..($j+=++$i)]}\n"}1..$a
Update: blakem pointed out that mine doesn't align the numbers. Darn.
Another update: I don't see any reason why any one of the following does not work, there seems to be a bug with the deprecated '$#' variable. In fact, the one with 's' segfaults on 5.6.1 (though if it worked, it would left justify the numbers). It woulda been 64 characters (if someone submits a patch to the perl core, they can have the credit for this :)
$#="%@{[2*length $a]}d";print map"@{[$j+1..($j+=++$i)]}\n",1..$a
$#="%@{[2*length $a]}u";print map"@{[$j+1..($j+=++$i)]}\n",1..$a
$#="%@{[2*length $a]}s";print map"@{[$j+1..($j+=++$i)]}\n",1..$a
Last Update: Thanks to help from master caddy
Albannach (for figuring out that 'f' works), 60 characters:
$#=" %@{[2*length$a]}.f";print map{$j+1..($j+=++$i),$/}1..$a
Really last update: Following the letter AND spirit of that last 'updated' condition, at 67 characters:
$#=" %@{[length int$a*$a/2]}.f";print map{$j+1..($j+=++$i),$/}1..$a
# or
$#=" %@{[length$a++*$a/2]}.f";print map{$j+1..($j+=++$i),$/}1..$a-1
# or
$#=" %@{[length$a*($a+1)/2]}.f";print map{$j+1..($j+=++$i),$/}1..$a
Tired of updating :) But more caddy help, this time from
blakem(65 characters):
$#=" %@{[length$a++*$a/2]}g";map{print$j+1..($j+=++$i),$/}1..$a-1
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.