U
usenet
I can create an array slice of another array, such as:
my @numbers = qw { 1 2 3 4 };
my @odds = @numbers[0,2];
But, suppose I have a hash like this:
my %number = (
'1' => 'one',
'2' => 'two',
'3' => 'three',
'4' => 'four',
);
And I want to create a subset of it (I don't want to use the term
'slice' here because in hash context it usually means just the values -
I want the keys/values).
I can do something like this:
my %odd;
@odd{1,3} = @number{1,3};
but I type the key list twice (or I must create an array to hold the
keys, and then type the name of the array twice). Anytime I see
something twice I wonder if it can be done differently... but I don't
see a way to avoid it here. Am I wrong?
I suppose I could do something really ugly like:
$odd{$_} = $number{$_} for (1, 3);
but I think I would rather see the list repetition than the loop. I
guess I'm just being pickey, but neither approach makes me want to
frame the code and put it on my wall.
my @numbers = qw { 1 2 3 4 };
my @odds = @numbers[0,2];
But, suppose I have a hash like this:
my %number = (
'1' => 'one',
'2' => 'two',
'3' => 'three',
'4' => 'four',
);
And I want to create a subset of it (I don't want to use the term
'slice' here because in hash context it usually means just the values -
I want the keys/values).
I can do something like this:
my %odd;
@odd{1,3} = @number{1,3};
but I type the key list twice (or I must create an array to hold the
keys, and then type the name of the array twice). Anytime I see
something twice I wonder if it can be done differently... but I don't
see a way to avoid it here. Am I wrong?
I suppose I could do something really ugly like:
$odd{$_} = $number{$_} for (1, 3);
but I think I would rather see the list repetition than the loop. I
guess I'm just being pickey, but neither approach makes me want to
frame the code and put it on my wall.