in reply to What is a "slice" ?

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

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

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