regex: how to %hash2 = grep {/KEY/} %hash1

G

Gerhard M

Hi,
currently i'm using

%what_i_want = map {$_=>$opts->{$_}} grep {/KEY/} keys %$opts;

or

%what_i_want = map {/KEY/? ($_=>$opts->{$_}) : ()} keys %$opts;

to extract some keys with it's values out of one hash.
Is there an easier/nicer way to do this?

Regards
Gerhard
 
P

Paul Lalli

Gerhard M said:
currently i'm using

%what_i_want = map {$_=>$opts->{$_}} grep {/KEY/} keys %$opts;
or
%what_i_want = map {/KEY/? ($_=>$opts->{$_}) : ()} keys %$opts;

to extract some keys with it's values out of one hash.
Is there an easier/nicer way to do this?

I'm not sure I understand exactly what you're looking for, but I *think*
you want to create a new hash with only the key/value pairs from the
original hash in which the key matches /KEY/. Is that correct?

I guess you could use a hash slice, but I'm no really convinced this is
either easier or nicer...

my %what_i_want = @$opts{grep /KEY/, keys %$opts};

Paul Lalli
 
G

Gunnar Hjalmarsson

Paul said:
I guess you could use a hash slice, but I'm no really convinced this is
either easier or nicer...

my %what_i_want = @$opts{grep /KEY/, keys %$opts};

It's not nicer, since it doesn't do what the OP wants.

This is a hash slice construct that works:

my %what_i_want;
@what_i_want{ grep /KEY/, keys %$opts } =
@$opts{grep /KEY/, keys %$opts};

but that's certainly not nicer either. ;-)
 
P

Paul Lalli

Gunnar Hjalmarsson said:
It's not nicer, since it doesn't do what the OP wants.

This is a hash slice construct that works:

my %what_i_want;
@what_i_want{ grep /KEY/, keys %$opts } =
@$opts{grep /KEY/, keys %$opts};

but that's certainly not nicer either. ;-)

Wow, I was completely off there. My apologies to the OP for not testing
my code before posting. Thanks Gunnar for the correction.

Paul
 
A

Anno Siegel

Paul Lalli said:
I'm not sure I understand exactly what you're looking for, but I *think*
you want to create a new hash with only the key/value pairs from the
original hash in which the key matches /KEY/. Is that correct?

I guess you could use a hash slice, but I'm no really convinced this is
either easier or nicer...

my %what_i_want = @$opts{grep /KEY/, keys %$opts};

The right hand side is a list of the *values* whose keys match /KEY/, in
unspecified order. Assigning that to a hash doesn't make sense.

Using a hash slice is possible, but unattractive:

my %what_i_want;
my @sel = grep /KEY/, keys %$opts;
@what_i_want{ @sel} = @$opts{ @sel};

Except when there are multiple selections to make from multiple hashes
(so that @sel could be re-used), I'd stick with the OP's first solution.

Anno
 
M

Matija Papec

X-Ftn-To: Gerhard M

Hi,
currently i'm using

%what_i_want = map {$_=>$opts->{$_}} grep {/KEY/} keys %$opts;

or

%what_i_want = map {/KEY/? ($_=>$opts->{$_}) : ()} keys %$opts;

to extract some keys with it's values out of one hash.
Is there an easier/nicer way to do this?

Not really,
$what_i_want{$_}=$opts->{$_} for grep /KEY/, keys %$opts;

but it could make sense to wrap your first example into function,
%what_i_want = MySlice(sub {/KEY/}, $opts);

#untested
sub MySlice {
my ($sub, $href) = @_;
my %hash =
map {$_=>$href->{$_}}
grep $sub->(),
keys %$href;
return wantarray ? %hash : \%hash;
}
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top