my @slice = @array[4..13];
In this code,
| @array | is an | array |
| @slice | is an | array |
| @array[4..13] | is a | slice |
| @array[4..13] | equals | ($array[4], $array[5], $array[6], $array[7], $array[8],
$array[9], $array[10], $array[11], $array[12], $array[13])
|
Note: every slice is also a list.
Juerd
# { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }
| [reply] [d/l] [select] |
my %month_hash = (
'1' => 'January',
'2' => 'February',
'3' => 'March',
'4' => 'April'
);
And since a hash doesn't come out in any meaningful predictable order - you created an array to get the order you wanted:
my @order = (1 .. 4);
Finally, for some reason you wanted to create a new array with the month names in it - you might try:
my @months = @month_hash{@order};
print $_,$/ for @months;
This would work unless one of your keys was deleted as such:
delete $month_hash{3};
my @months = @month_hash{@order};
print $_,$/ for @months;
You would then get an error about an uninitialized value. This happens because the hash slice creates a new key for '3' and sets its value to undef.
Cheers - L~R | [reply] [d/l] [select] |
You would then get an error about an uninitialized value. This happens because the hash slice creates a new key for '3' and sets its value to undef.
You will get a warning (if you're running with warnings enabled, that is), but only because you are using an uninitialized value, not because a new element is auto-vivified in the hash. If any of the keys in your hash slice does not exist, the hash slice will put an undef in that place in the resulting list. So in this case @months will get ('January', 'February', undef, 'April'), and you'll get a warning when you try to print the undef. But %month_hash remains the same, there is still no element with key '3'.
Auto-vivification can be a problem when you are referencing a nested structure, as in the following, albeit contrived, example:
my %HoA = (
'1' => [ 'January', 'Januare' ],
'2' => [ 'February', 'Februare' ],
'4' => [ 'April' , 'Avril' ],
);
for (1 .. 4)
{
print "English: $HoA{$_}[0], French: $HoA{$_}[1]", $/;
}
# watch out! $HoA{3} has been auto-vivified as an ARRAY-ref!!
HTH
-- 3dan | [reply] [d/l] |