in reply to (OT?) n(n-1)/2

If you're just doing this for Perl education, don't bother with useless mathematical optimizations. In real life, you can often sacrifice a bit of speed for ease of coding.

my $a = 0; $a += $_ for 1..1000; print "Answer: $a\n";

If this isn't about Perl programming, or about programming at all, you're posting in the wrong place :)

P.S. With Perl Golf, of course mathematic principles are a lot more important: $foo && ($foo % 9 || 9) is shorter than substr($foo, 0, 1) + substr($foo, 1, 1) (and do it again if the answer's >9)

++ vs lbh qrpbqrq guvf hfvat n ge va Crey :)
Nabgure bar vs lbh qvq fb jvgubhg ernqvat n znahny svefg.
-- vs lbh hfrq OFQ pnrfne ;)
    - Whreq

Replies are listed 'Best First'.
•Perl Puzzle, was Re: (OT?) n(n-1)/2
by merlyn (Sage) on Mar 03, 2002 at 13:07 UTC
    Observe magic for the perl puzzle of the day, based on the math puzzle:
    #!/usr/bin/perl -w use strict; # my $a = 0; ## notice this is deleted! $a += $_ for 1..1000; print "Answer: $a\n";
    Points to ponder:
    1. Why does this even compile? It's use strict!
    2. Why no warnings? We're using undef in math!
    3. How come it still gets the same result? We didn't initialize the variable!
    4. Bonus question: can you tell why we removed all uses of $a as a casual variable from the third edition of Learning Perl?
    Hmm. {grin}

    -- Randal L. Schwartz, Perl hacker

      The questions are far too easy :)
      1. $a is a global that can be used without declaring it because sorting would otherwise be a pain in the but.
      2. += is a "don't care" operator. $a = $a + $_ would have generated a "use of uninitialized value in ..."-warning.
      3. See 1 and 2
      4. See 1 - you probably tried to avoid confusion, which is a good thing. Perhaps I should have done so to, but I could really not care less for a three-liner.

      ++ vs lbh qrpbqrq guvf hfvat n ge va Crey :)
      Nabgure bar vs lbh qvq fb jvgubhg ernqvat n znahny svefg.
      -- vs lbh hfrq OFQ pnrfne ;)
          - Whreq
      

      1., 2. see perlman strict, chapter 'strict vars'
      as in a previuos thread already mentioned :-)
      $a and $b arre exempted from the strict vars pragma due to their special role in the sort function
      3. undef equals to '0'
      4. dunno, didn't read it yet :-)

      Have a nice day
      All decision is left to your taste

        3. undef equals to '0'

        No.
        undef evaluates to 0 in numeric context, to an empty string in string context. In boolean context, it is false. However, undef + 0 would eq '0', because first 0 + 0 is calculated, and then the result (0) is put in string context, and will be the string 0, which is in every way identical to 0 :)

        When undef is used with a numeric (+, -, * etcetera) or string operator (., x, =~, etc) that is not an assignment (+=, -=, ++, etc) itself, a warning will be emitted.

        ++ vs lbh qrpbqrq guvf hfvat n ge va Crey :)
        Nabgure bar vs lbh qvq fb jvgubhg ernqvat n znahny svefg.
        -- vs lbh hfrq OFQ pnrfne ;)
            - Whreq
        

        Read question 2 more carefully.

        Question 1 has to do with $a passing "use strict" for the reason you offer.

        But question 2 has to do with warnings on math with undef values. Although $a passes strict in this puzzle, it is still undef at the point we first encounter it.

        Just about any kind of math on such a value will not abort (as happens with the use strict violation referred to in Q. 1), but it normally does produce a warning (if warnings are turned on) e.g.:

        $a = $a + 1; # warns but executes anyway print $a; # 1
        In this case, the warning is: "Use of uninitialized value in addition (+) at testprog.pl line xx.". So question 2 is: Why is such a warning not produced in the snippet meryln refers to.