in reply to What is a "slice" ?

A slice is another collection of elements, not just one. For example:
my @array = ('a'..'z'); my @slice = @array[4..13];
Now @slice contains the 4th through the 13th elements of @array.

A hash slice is very similar, it's just a possible subset of the original container. Hash slices can be used to create hashes from two arrays:

my @keys = qw(foo bar baz); my @values = qw(one two three); my %hash; # have to declare first! @hash{@keys} = @values; # notice the @ instead of % use Data::Dumper; print Dumper \%hash; __END__ $VAR1 = { 'foo' => 'one', 'baz' => 'three', 'bar' => 'two' };
Hope this helps. :)

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

Replies are listed 'Best First'.
Re: Re: What is a "slice" ?
by Juerd (Abbot) on Aug 23, 2003 at 16:10 UTC

    my @slice = @array[4..13];

    In this code,
    @arrayis anarray
    @sliceis anarray
    @array[4..13]is aslice
    @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' }

Re: Re: What is a "slice" ?
by Limbic~Region (Chancellor) on Aug 23, 2003 at 16:11 UTC
    jeffa,
    I know you know this, but since you are running off to a rock concert I thought it would be worth while to point out a potential "gotcha" with un-intentional auto-vivication. Of course I have broken it down to an extreme for the sake of the OP.

    Say you had a hash set up as:

    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

      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