157 characters... ;-)
select *I=*STDERR;$|++;select STDOUT;$_=join('',<DATA>);
s#\[([^\]]*\?)\]#print I"$1 ";chomp($i=<DATA>);$i#ge;
s#\[([^\]]*)\]#@f=split(/\|/,$1);$f[rand@f]#ge;print
Update: admittadly, I suck at golf ;/
Yet, just now I've learnt an important lesson. The only reason I failed to produce significantly shorter/improved golf code is because I simply failed to think 'outside of the box'. For a couple minutes I simply kept staring at the initial golf piece, in the back of my mind being afraid to temper with it too much as not to break it.
Had I only taking the logic of the code aside and tried to come up with an alternative code of mine, I could have a better chance at gaining significant improvement.
{grin}
Update 1:
Looking at
Util's and
japhy's golf code and sort of combining the good parts of both, I get this piece 97 characters long:
print map{s/\?$/? /?print(STDERR)&&(<>=~/.*/g)[0]:split('\|')&&$_[rand
+@_]}
map{split/\[(.+?)\]/}<>
Update 2:
japhy, the golf works fine for me, here's the complete code and source file:
#!/usr/local/bin/perl
print map{s/\?$/? /?print(STDERR)&&(<>=~/.*/g)[0]:split('\|')&&$_[rand
+@_]}
map{split/\[(.+?)\]/}<>
Source file:
Hello [world|planet|dude|there], [Name?]!
Hmm,.. but it does appear to print extra '0's when I use the original text. Let me investigate why that's the case ...
update: This works:
print map{if(s/\?$/? /){print STDERR;(<>=~/.*/g)[0]}@z=split/\|/;$z[ra
+nd@z]}
map{split/\[(.+?)\]/}<>
_____________________
# Under Construction
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.