Hello I'm trying to use the assignment hyper operator in Perl 6
https://docs.perl6.org/language/operators#Hyper_operators
my (@one, @two) «=» (<1 2 3>,<4 5 6>);
say @one;
say @two;
# Prints nothing (empty array)
# Expected behaviour:
@one=<1 2 3>;
@two=<4 5 6>;
say @one;
say @two;
# Prints [1 2 3] and [4 5 6]
How to make the assignment hyper operator operate correctly? Thanks.
Here 4 and 5 are called operands and + is called operator. Perl language supports many operator types, but following is a list of important and most frequently used operators − Lets have a look at all the operators one by one.
Perl arithmetic operators deal with basic math such as adding, subtracting, multiplying, diving, etc. To add (+ ) or subtract (-) numbers, you would do something as follows: To multiply or divide numbers, you use divide (/) and multiply (*) operators as follows:
Here's what perlhas that C doesn't: The exponentiation operator. The exponentiation assignment operator. The null list, used to initialize an array to null. Concatenation of two strings. The concatenation assignment operator. eq String equality (== is numeric equality). For a mnemonic just think of "eq" as a string.
A Perl list is a sequence of scalar values. You use parenthesis and comma operators to construct a list. Each value is the list is called list element. List elements are indexed and ordered. You can refer to each element by its position. The first list () is an empty list. The second list (10,20,30) is a list of integers.
You were close. Just a little bit further in the docs down we find
The zip metaoperator (which is not the same thing as Z) will apply a given infix operator to pairs taken one left, one right, from its arguments.
So
my (@one, @two) Z= <1 2 3>, <4 5 6>;
Here's a benchmark, running on the current developer build. It compares the "magic" variant above with two sequential assignments.
use v6;
use Benchmark;
my %results = timethese(100000, {
"magic" => sub { my (@one, @two) Z= <1 2 3>, <4 5 6> },
"plain" => sub { my @one = <1 2 3>; my @two = <4 5 6> },
});
say ~%results;
# magic 1569668462 1569668464 2 0.00002
# plain 1569668464 1569668464 0 0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With