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)
| [reply] [d/l] [select] |
|
|
| [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] |
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.
| [reply] |
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 | [reply] [d/l] [select] |
|
|
| [reply] [d/l] [select] |
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. | [reply] |
|
|
| [reply] |
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).
| [reply] |