Terrible at introductions so let's hop right to it
Posted: Mon Oct 09, 2017 2:02 am
As Doug phrased it, I'm a late member of the flood from facebook after that vice video about him. If I haven't already lost you let's move on.
So I fell across this old post viewtopic.php?f=13&t=940 and Doug showed off the succinctness and some may say the beauty of Perl. The final, shortest solution being
#!/usr/bin/perl
$\ = $/;
for (0 .. 9) {print}
So of course I was left wondering could this be shortened. But before moving ahead, the length of that solution with all unnecessary whitespace removed is as follows
$\=$/;for(0..9){print}
My first shortening was to move the print to before the for loop (don't recall the name of this coding format, let's call it statement prior to control) so we can drop the curlies around the print and the parentheses on the for loop condition
$\=$/;print for 0..9
Remaining with this code format we can drop the assignment and instead explicitly detail what is to be printed. You'll notice the typical whitespace between print and the double qoute and between double quote and for is actually optional
print"$_$/"for 0..9
Lastly, we can save another character by dropping the quotes and concatenate the strings using period
print$_.$/for 0..9
And now in long form :-)
#!/usr/bin/perl
print $_ . $/ for 0 .. 9
Here's an attempt with an alternative approach which performs one print statement for all 10 lines by joining the range/array together with the new line character sequence. The blank string literal is included since the new line character sequence is only used to join two adjoining array elements, meaning no new line would be appended after the 9 if the blank weren't included
print join $/,(0..9,"")
However as you can see this is actually longer than what we'd started with, so much for that! Now, perhaps we get even shorter by having Perl write out and execute machine code... but can you really say that's a Perl solution
So I fell across this old post viewtopic.php?f=13&t=940 and Doug showed off the succinctness and some may say the beauty of Perl. The final, shortest solution being
#!/usr/bin/perl
$\ = $/;
for (0 .. 9) {print}
So of course I was left wondering could this be shortened. But before moving ahead, the length of that solution with all unnecessary whitespace removed is as follows
$\=$/;for(0..9){print}
My first shortening was to move the print to before the for loop (don't recall the name of this coding format, let's call it statement prior to control) so we can drop the curlies around the print and the parentheses on the for loop condition
$\=$/;print for 0..9
Remaining with this code format we can drop the assignment and instead explicitly detail what is to be printed. You'll notice the typical whitespace between print and the double qoute and between double quote and for is actually optional
print"$_$/"for 0..9
Lastly, we can save another character by dropping the quotes and concatenate the strings using period
print$_.$/for 0..9
And now in long form :-)
#!/usr/bin/perl
print $_ . $/ for 0 .. 9
Here's an attempt with an alternative approach which performs one print statement for all 10 lines by joining the range/array together with the new line character sequence. The blank string literal is included since the new line character sequence is only used to join two adjoining array elements, meaning no new line would be appended after the 9 if the blank weren't included
print join $/,(0..9,"")
However as you can see this is actually longer than what we'd started with, so much for that! Now, perhaps we get even shorter by having Perl write out and execute machine code... but can you really say that's a Perl solution