Perl adding array with another array

I cam across the code below online where it's trying to add two array. Can anyone explain what it is calculating to get 14?

my @a = (1,2,5)+(8,9);
print "@a";

output: 14

Answers


Output is 14 as $a[0] is 14 => 5+9

+ operator imposes scalar context on both lists so last elements are taken and added,

# in scalar context $x is assigned with last element
my $x = (1,2,5);
print "\$x is $x\n";

outputs $x is 5

warnings pragma would also complain, giving you a hint that something fishy is going on,

Useless use of a constant (8) in void context

Starting with:

my @a = (1,2,5)+(8,9);

When using a list in a scalar context, the last element is returned. Consult What is the difference between a list and an array? for details.

Therefore the above two lists reduce to:

my @a = 5 + 9;

Which mathematically equals:

my @a = (14);

Need Your Help

Cabal tests on windows 7

windows haskell cabal

I am trying to follow the examples from here http://www.haskell.org/cabal/users-guide/#test-suites

using sql - Is not null in a select statement

sql sql-server null isnull ifnull

I can't seem to figure out how to use the opposite of isnull or ifnull statements in sql. I need to say if a.Error1 is not null -- then print the ' - ' and the + CHAR(13)+CHAR(10). Basically There ...