No one could come up with the correct way to do multiplication tables in Perl? Here's a little snippet I've found useful over the years. It's evolved somewhat, but thanks to Dominus and his Program Repair Shop, I've managed to reduce some of the extra clutter:
#!/usr/bin/perl -w use strict; @ARGV=(); $; = "%2u * %2u = %u"; ($_ = $. = <>); do { chomp; $! = !(defined($_)) } while ($|); print join($/, map{ $!++; sprintf($;,$!,$_, ($_ * $!)) } (($.) x 10));
If you don't know how to do something, just say so. Don't pick on the original poster. :)

Replies are listed 'Best First'.
(jptxs)Re: I Can't Believe You People (tm)
by jptxs (Curate) on Jan 12, 2001 at 22:12 UTC
    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
      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
Re (tilly) 1: I Can't Believe You People (tm)
by tilly (Archbishop) on Jan 12, 2001 at 22:10 UTC
    I tried to construct a multiplication for 4.4 and was surprised by the result. The one for -5 was even more shocking.
Re: I Can't Believe You People (tm)
by zigster (Hermit) on Jan 12, 2001 at 22:02 UTC
    *clap clap*
    Nice one, I will have to return here for a ++ on monday .. cool as
    --

    Zigster