Reputation: 3843
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.
Upvotes: 1
Views: 126
Reputation: 755010
It is not valid Perl as written:
@{$labels->{$set}->{"train"}->{"negative"}} = (@{$labels->{$set}->{"train"}->{"negative"}};
^
Syntax error - open parenthesis without a matching close parenthesis
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:
(@{$labels->{$set}->{"train"}->{"negative"}}, splice(@shuffled, 0, 2000));
The parentheses provide an array or list context; the first term is the original array; the second term is the result of splice
applied to the array @shuffled
. So, the splice
removes a couple thousand elements (2001?) from the array @shuffled
and 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:
push @{$labels->{$set}->{"train"}->{"negative"}}, splice(@shuffled, 0, 2000);
It's also more economical on the typing, and a lot more economical on the brain cells.
Upvotes: 4
Reputation: 67910
The statement:
@{$labels->{$set}->{"train"}->{"negative"}} =
(@{$labels->{$set}->{"train"}->{"negative"}}, splice(@shuffled, 0, 2000));
Does a number of things at once. It can also be written, slightly more verbose:
my @array = @{$labels->{$set}->{"train"}->{"negative"}};
my @values = @shuffled[0..1999]; # get the first 2000 values
splice @shuffled, 0, 2000; # delete the values after use
@array = (@array, @values); # add the values to the array
@{$labels->{$set}->{"train"}->{"negative"}} = @array;
As you'll notice, the LENGTH in splice is not the number of the array element, but the length of the array, which is why the count is one off in the array slice above.
As Jonathan pointed out, it is much simpler to use push.
push @{$labels->{$set}{"train"}{"negative"}}, splice(@shuffled, 0, 2000);
Documentation: splice
Upvotes: 2
Reputation: 1146
$labels
is a reference to a hash of hashes with three depths (HoHoH). The lookup $labels->{$set}->{"train"}->{"negative"}
returns an array reference.
Hope that helps a bit ..
Upvotes: 0