9.5. The x operator
The expression (@array) x $num_times returns an array that is composed of $num_times copies of @array one after the other. The expression $scalar x $num_times, on the other hand, returns a string containing $num_times copies of $scalar concatenated together string-wise.
Therefore it is important whether the left operand is wrapped in parenthesis or not. It is usually a good idea to assign the left part to a variable before using x so you'll have the final expression ready.
Here's an example to illustrate the use:
print "Test 1:\n"; @myarray = ("Hello", "World"); @array2 = ((@myarray) x 5); print join(", ", @array2), "\n\n"; print "Test 2:\n"; @array3 = (@myarray x 5); print join(", ", @array3), "\n\n"; print "Test 3:\n"; $string = "oncatc"; print (($string x 6), "\n\n"); print "Test 4:\n"; print join("\n", (("hello") x 5)), "\n\n"; print "Test 5:\n"; print join("\n", ("hello" x 5)), "\n\n";
Can you guess what the output of this program will be?
Here's a spoiler
Test 1: Hello, World, Hello, World, Hello, World, Hello, World, Hello, World Test 2: 22222 Test 3: oncatconcatconcatconcatconcatconcatc Test 4: hello hello hello hello hello Test 5: hellohellohellohellohello