in reply to (Golf) Mad-Lib Generator

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

Replies are listed 'Best First'.
Re: Re: (Golf) Mad-Lib Generator
by japhy (Canon) on Jul 17, 2002 at 18:43 UTC
    I get zeroes in the output when I use this.

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a job (NYC-area)
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;