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

Discussion in 'Perl Misc' started by Gerhard M, Jan 17, 2005.

  1. Gerhard M

    Gerhard M Guest

    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
     
    Gerhard M, Jan 17, 2005
    #1
    1. Advertising

  2. Gerhard M

    Paul Lalli Guest

    "Gerhard M" <> wrote in message
    news:...
    > 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
     
    Paul Lalli, Jan 17, 2005
    #2
    1. Advertising

  3. Paul Lalli wrote:
    > 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. ;-)

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Jan 17, 2005
    #3
  4. Gerhard M

    Paul Lalli Guest

    "Gunnar Hjalmarsson" <> wrote in message
    news:...
    > Paul Lalli wrote:
    > > 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. ;-)


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

    Paul
     
    Paul Lalli, Jan 17, 2005
    #4
  5. Gerhard M

    Anno Siegel Guest

    Paul Lalli <> wrote in comp.lang.perl.misc:
    > "Gerhard M" <> wrote in message
    > news:...
    > > 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};


    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
     
    Anno Siegel, Jan 17, 2005
    #5
  6. Gerhard M

    Matija Papec Guest

    X-Ftn-To: Gerhard M

    (Gerhard M) wrote:
    >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;
    }


    --
    Matija
     
    Matija Papec, Jan 17, 2005
    #6
  7. Gerhard M

    Guest

    what about this:
    for(%$opts) {%what_i_want{$_} = $opts->{$_} if /KEY/}


    wana
     
    , Jan 17, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christian Bongiorno

    custom key and hasmap using a ranged key

    Christian Bongiorno, Jun 15, 2004, in forum: Java
    Replies:
    1
    Views:
    466
    Roedy Green
    Jun 15, 2004
  2. Patrick Guio
    Replies:
    6
    Views:
    3,234
    chris
    Oct 20, 2004
  3. Replies:
    3
    Views:
    798
    Reedick, Andrew
    Jul 1, 2008
  4. M P
    Replies:
    1
    Views:
    504
  5. viki
    Replies:
    14
    Views:
    259
    Ben Bullock
    Jun 20, 2008
Loading...

Share This Page