Creating a subset of a hash?

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.
 
A

axel

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?

You could assign the key list to an array...

my @klist = qw/1 3/;
my %odd;
@odd{@klist} = @number{@klist};

Axel
 
P

Paul Lalli

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.

Heh. David seems to be having a brain-cramp today. ;-)

my %odd = map { $_ => $number{$_} } 1, 3;

Paul Lalli
 
U

usenet

Paul said:
Heh. David seems to be having a brain-cramp today. ;-)

Of course - why should today be any different than any other day?
my %odd = map { $_ => $number{$_} } 1, 3;

D'oh. I keep thinking of map in plain array context and forget how
useful it can be with hashes. Thanks!
 
D

Dr.Ruud

(e-mail address removed) schreef:
I suppose I could do something really ugly like:

$odd{$_} = $number{$_} for (1, 3);

What is ugly about that?

$odd{$_} = $number{$_} for qw/1 3/ ;
 
U

usenet

Dr.Ruud said:
What is ugly about that?

$odd{$_} = $number{$_} for qw/1 3/ ;

I guess it's because my mental parser hits the line multiple times.
map() is probably doing the same type of thing under the covers, but my
mind-parser only hits map() once.

Like I said, I'm probably just being too picky.... but I learn stuff
when I ask about alternate approaches.
 
D

Dr.Ruud

(e-mail address removed) schreef:
Dr.Ruud:

I guess it's because my mental parser hits the line multiple times.

I often prefer "for" to "map", but nothing beats a single clear line.


A "map EXPR,LIST" alternative:

my %odd = map +( $_ => $number{$_} ), qw/1 3/ ;


A "map EXPR BLOCK" alternative:

my %odd = map { $_ => $number{$_} } qw/1 3/ ;


For fun:

my %odd = map { ($_*=2)+=1 => $number{$_} } -2..5 ;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top