I am studying an existing Perl program, which includes the following line of code:
@{$labels->{$set}->{"train"}->{"negative"}} = (@{$labels->{$set}->{"train"}->{"negative"}}, splice(@shuffled, 0, 2000));
I am very confused on how to understand this piece of code.
It is not valid Perl as written:
If you ignore the open parenthesis, then the LHS and the RHS expressions are identical; it assigns an array value to itself. The arrows
->and{}notations mostly mean that you’re dealing with an array ref at the end of a hash reference to a hash reference to a hash reference, or thereabouts. It is a nasty piece of code to have to understand, at best, but the structure may make more sense in the bigger context of the whole program (and the whole program will be considerably bigger, so I don’t recommend posting it here).Double check your copy’n’paste. If that is actually in the Perl script, then it can’t be being compiled, much less executed, so you’ll have to work out how and why that line is not operative.The revised expression has the RHS:
The parentheses provide an array or list context; the first term is the original array; the second term is the result of
spliceapplied to the array@shuffled. So, thespliceremoves a couple thousand elements (2001?) from the array@shuffledand the expression as a whole adds the deleted elements to the end of the array identified by the complex expression on the LHS.It would probably be more efficiently written as:
It’s also more economical on the typing, and a lot more economical on the brain cells.