Hayl_ has asked for the wisdom of the Perl Monks concerning the following question:

I have read the Llama book and now I am reading (slowly) the Camel book. They keep on talking about array and hash "slices" but they don't explain what they mean. Is a slice just an element in an array or a hash, or is it more than that? I did a search here but didn't see anything with an explanation of what a slice is... :(

Replies are listed 'Best First'.
Re: What is a "slice" ?
by jeffa (Bishop) on Aug 23, 2003 at 15:57 UTC
    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)
    

      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' }

      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
Re: What is a "slice" ?
by BrowserUk (Patriarch) on Aug 23, 2003 at 15:59 UTC

    Try this link as a starting place. slices.

    If you still have specific questions, come on back:)


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
    If I understand your problem, I can solve it! Of course, the same can be said for you.

Re: What is a "slice" ?
by davido (Cardinal) on Aug 23, 2003 at 16:19 UTC
    Think of a slice as being a specific subset of an array or hash.

    my @array = (1, 2, 3, 4, 5); my @array_slice = @array[0,1,2]; print @array,"\n"; print @array_slice,"\n";

    Slices can also be used with hashes in similar fashion. Oh, and they can be used with literal lists as well.

    A slice is a new subset list, based on the contents of elements of another list.

    UPDATE The arrays in the preceeding examples are simply containers for the data. And the containers are given names that reasonably illustrate what is going on. @array_slice contains a slice that came out of @array. @array_slice is, itself, an array as well.

    @array[1,2,3,4] # a slice from an array. @array[1..4] # a slice of sequential elements # from an array. (1,2,3,4)[0,1,2] # a slice of a list. @hash{'Monday', 'Tuesday'} # a slice from a hash.

    Dave

    "If I had my life to do over again, I'd be a plumber." -- Albert Einstein

      my @array = (1, 2, 3, 4, 5); my @array_slice = @array[0,1,2]; print @array,"\n"; print @array_slice,"\n";

      In this code,
      @arrayis anarray
      @array_sliceis anarray
      @array[0,1,2]is aslice
      @array[0,1,2]equals($array[0], $array[1], $array[2])
      @array,"\n"is alist
      @array_slice,"\n"is alist
      Note: every slice is also a list.

      Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

Re: What is a "slice" ?
by Hayl_ (Acolyte) on Aug 23, 2003 at 16:11 UTC
    thanks everyone for your replies. they are all great.

      You also might want to have a look at one of my nodes. It has wisdom directly from the Camel book. :)

      --
      Allolex

In defence of the Llama...
by jonnyfolk (Vicar) on Aug 24, 2003 at 08:08 UTC
    Try going back to the Llama and looking up 'slices' in the index. You'll find a pretty complete explanation of the various types of slice (pp 242 -247).