in reply to Re: Re: Summing the elements of multiple arrays into a new array
in thread Summing the elements of multiple arrays into a new array

mull, you're absolutely right. The reason I didn't was that I assumed the compiler would squeal about undefined values if I tried to add a value that didn't exist. But of course it doesn't - what with this being Perl, and all. ;)

So

$c[@c] = $a[@c] + $b[@c] while defined $a[@c] or defined $b[@c];

andy.

  • Comment on Re: Re: Re: Summing the elements of multiple arrays into a new array
  • Download Code

Replies are listed 'Best First'.
Re4: Summing the elements of multiple arrays into a new array
by blakem (Monsignor) on Nov 02, 2001 at 01:25 UTC
    If you have -w or use warnings turned on, you'll get the following error when the arrays aren't the same size:
    Use of uninitialized value in addition (+)
    Which is the most irritating non-warning that perl can toss at you. Its complaining about something like (3 + undef) which is a perfectly valid Perl expression, so I wish perl wouldn't yell at me for using it.

    p.s. my solution above also spits out the same spurious warnings...

    -Blake

      It's a good warning in the sense that sometimes you will have variables that you intended to set get into your assignments still undefined and this lets you know, in a non-fatal way, that something may be wrong.

      On the other hand, you can do like the Slashdot crew and customize a SIGWARN callback to muffle this output.
        No need to go through all that on current versions of perl.... If I know I'm in a 5.6.0 or newer perl environment, the first few lines of my code are usually:
        #!/usr/bin/perl -T use warnings; no warnings "uninitialized";
        I find that this bounds me enough to catch my screw-ups, but not enough to inhibit my coding style..

        -Blake