in reply to Troubles with Unshift and Push
The bracket operator around data creates an array reference.
my @num_weights = [36,264,188];
At this point, @num_weights has a single scalar element, which is itself a reference to another array.
Same as:
my @sub_array = (36,264,188); ##real array my @num_weights = (\@sub_array); ##real array containing 1 reference
Btw backslash is the make-reference operator
Use parentheses to declare your arrays and it should work.
This modified code:
my @num_weights = (36,264,188); my @leftovers = (1,2,3,4); my @hold_generations; my $i = 0; for(my $p = 0; $p < 1;$p++) #Go through all leftovers { my $pick = $leftovers[$p]; unshift @num_weights, $pick; #add item say Dumper(@num_weights); }
Prints:
$VAR1 = 1; $VAR2 = 36; $VAR3 = 264; $VAR4 = 188;
|
|---|