Comparing all hash keys to a string

Discussion in 'Perl Misc' started by dalyea@gmail.com, Apr 18, 2009.

  1. Guest

    I frequently do something like this:

    foreach my $key (keys %hash) {
    if ($string=~/$key/i) {
    do_something;
    }
    }

    Is there an easy and fast and more compact way to compare all
    the keys of a hash to a string? Something like:

    my @found=grep /$_/ $string, keys %hash;

    where @found would contain all the matching keys from the
    hash found in $string.

    Thanks, I've always wanted to find a better way to do this.
    , Apr 18, 2009
    #1
    1. Advertising

  2. schrieb:
    > I frequently do something like this:
    >
    > foreach my $key (keys %hash) {
    > if ($string=~/$key/i) {
    > do_something;
    > }
    > }
    >
    > Is there an easy and fast and more compact way to compare all
    > the keys of a hash to a string? Something like:
    >
    > my @found=grep /$_/ $string, keys %hash;
    >
    > where @found would contain all the matching keys from the
    > hash found in $string.
    >
    > Thanks, I've always wanted to find a better way to do this.


    What you try to do remembers me to a pointer function call like in C.

    The best would to put the function address of "do_something();" function
    as value in the key of your hash table:

    Then instead of the foreach and if calls simply execute:

    $key{$string}();

    It would be more faster than to parse strings. But your string should
    then match exactly the entry in the hash table.

    I'm not sure if its this what you want.

    --
    Best regards,
    Anton
    E-Mail:
    Anton Ramunda, Apr 18, 2009
    #2
    1. Advertising

  3. <> wrote:
    > I frequently do something like this:
    >
    > foreach my $key (keys %hash) {
    > if ($string=~/$key/i) {
    > do_something;
    > }
    > }
    >
    > Is there an easy and fast and more compact way to compare all
    > the keys of a hash to a string? Something like:
    >
    > my @found=grep /$_/ $string, keys %hash;
    >
    > where @found would contain all the matching keys from the
    > hash found in $string.



    my @found = map {$string =~ /$_/gi} keys %hash;
    or
    my @found = map {$string =~ /\b$_\b/gi} keys %hash;


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
    Tad J McClellan, Apr 18, 2009
    #3
  4. Ben Bullock Guest

    On Sat, 18 Apr 2009 17:54:39 -0500, Tad J McClellan wrote:

    > my @found = map {$string =~ /$_/gi} keys %hash;
    > or
    > my @found = map {$string =~ /\b$_\b/gi} keys %hash;


    This is a very compact solution. Another possibility which might be
    faster if you have a lot of keys and the hash does not change is to build
    a regular expression from the hash keys as follows:

    #!/home/ben/software/install/bin/perl
    use warnings;
    use strict;
    my %hash = qw/ba be bu be bo ca di du de do Monster Monkey Brad Pitt/;
    # Long version
    my @sortedbylength = sort {length($b) <=> length($a)} keys %hash;
    my $matchingexpression = '('.join ("|", @sortedbylength).')';
    print "$matchingexpression\n";
    my $string = "ba bill bo brad tiny monster";
    my @matches = ($string =~ /$matchingexpression/gi);
    print "@matches\n";
    # Shorter version
    my $m='('.join ('|',(sort {length $b <=> length $a} keys %hash)).')';
    my @matches2 = ($string =~ /$m/gi);
    print "@matches2\n";
    Ben Bullock, Apr 19, 2009
    #4
    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. rp
    Replies:
    1
    Views:
    499
    red floyd
    Nov 10, 2011
  2. Xeno Campanoli
    Replies:
    16
    Views:
    257
    Martin DeMello
    Aug 25, 2005
  3. Alex Fenton

    Hash#values and Hash#keys order

    Alex Fenton, Apr 7, 2006, in forum: Ruby
    Replies:
    1
    Views:
    131
    George Ogata
    Apr 15, 2006
  4. Mage

    hash.keys and hash.values

    Mage, Aug 13, 2006, in forum: Ruby
    Replies:
    14
    Views:
    170
  5. Jason
    Replies:
    8
    Views:
    131
    Dr.Ruud
    Jul 27, 2006
Loading...

Share This Page