in reply to I Can't Believe You People (tm)

First, this seems to break on numbers which are >= 10 digits.

Second, could you break down what this is doing a bit for the dumber of us as it's really cool. I've looked up what $; is and such, but I have to say I just don't see it. Any explanation at all would be highly appreciated.

"A man's maturity -- consists in having found again the seriousness one had as a child, at play." --Nietzsche

Replies are listed 'Best First'.
Re: (jptxs)Re: I Can't Believe You People (tm)
by chromatic (Archbishop) on Jan 13, 2001 at 00:50 UTC
    It's about half-obfuscated, deliberately. I didn't really care which magic variables I used, as they were mostly all good candidates (don't need to be declared, a first year student probably wouldn't know anything about them).

    Here's my explanation:

    @ARGV=();

    I use the magic input operator in a moment to read from STDIN. This'll break if @ARGV has anything, so I clobber any command line arguments.

    $; = "%2u * %2u = %u";

    I'd have to look up $; to see what it does. It doesn't break anything, so why not use it here? This is just a format string for printf. It's also the source of the apparent bugs jptxs and tilly mention.

    ($_ = $. = <>);

    A little bit of misdirection here. Would a beginning programmer know about $. or the magic input? I think not.

    do { chomp; $! = !(defined($_)) } while ($|);

    This just gets rid of the newline on $_ and sets $! to 0 ($_ is most likely defined, so I negate the result of that check). $| is 0, so it only executes once.

    print join($/, map{ $!++; sprintf($;,$!,$_, ($_ * $!)) } (($.) x 10));

    Here's the real meat. First, we create a ten-element list of the input. There's another subtle bug that doesn't hurt any, because $. hasn't been chomped. Perl does what I mean, though.

    Next, we increment $!, which we use in the multiplier. It's at zero to start, so we need to get it to 1 before looping.

    The map returns a list of strings from sprintf, with the format slots filled. The join puts them together with the contents of $/ (a newline, as we haven't messed with it) and they're printed.

    There you have it. A mess of punctuation with concepts a beginning programmer wouldn't know but bugs he'd be likely to produce. Now I have to go take a shower.

      well, my problem was that I did look up what $; did and as a result I thought you were using some deep deep magic in your sprintf that I had never seen. actually you were using silly magic (in the form of feeding sprintf a format in a variable) that I had never seen before. Very good, then. As you were. =)
      "A man's maturity -- consists in having found again the seriousness one had as a child, at play." --Nietzsche