Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: text string approxiamtions (concept for review)

by seattlejohn (Deacon)
on Dec 05, 2002 at 03:48 UTC ( [id://217670]=note: print w/replies, xml ) Need Help??


in reply to text string approxiamtions (concept for review)

Be sure to look at String::Approx too.

A cool thing that hashes do not do, would be to allow a regex for a key.

I can't speak for earlier versions, but you can definitely use regexes as hash keys in 5.6.1:

use strict; my %tests = ( qr/^[0-9]+$/ => 'positive integer', qr/co/i => 'company/corporation', ); foreach my $term qw(12345 Company Jones) { foreach my $key (keys %tests) { print "$term is a $tests{$key}\n" if $term =~ $key; } }

        $perlmonks{seattlejohn} = 'John Clyman';

Replies are listed 'Best First'.
Re: Re: text string approxiamtions (concept for review)
by shemp (Deacon) on Dec 05, 2002 at 16:46 UTC
    I poorly stated my intentions for regexs with hashes. What i want is to be able to use a regex as a lookup of values in the hash. For instance:
    my %names = ( "sean" => 29, "lisa" => 14, "bob" => 25, "matt" => 23, "tom" => 52 ); my @matches = %names{/a/};
    Here, the regex /a/ would match the keys "sean", "lisa", and "matt".
    so matches would contain the cooresponding values 29, 14, 23

    It would also be cool to be able to get the matched keys, so something like
    my @keys = keys %names{/a/};
    Then @keys would contain the array of matched keys: "sean", "lisa", "matt"

    I'm making up the syntax, but i think this better describes what i would like to be able to do.

      grep and map can probably help. First, to get a subset of hash keys:
      my @a_keys = grep {/a/} (keys %names);

      Then to get their values:
      my @a_values = map {$names{$_}} grep {/a/} (keys %names);

      You could also get the job done using hash slices, like so:
      my @a_values = @names{grep {/a/} keys %names};

      That's more like the syntax you proposed, though I find it a bit disconcerting (maybe because I don't find the @names{...} hash-slice notation particularly natural in the first place).

              $perlmonks{seattlejohn} = 'John Clyman';

      Update: only answered half the question in my original reply. Fixed here.

        I guess i was really throwing this idea to the monks as something on my wishlist to be internally optimized by Perl's hash implementation. What has been suggested will certainly work, but i was trying to imply that if these sort of operations were part of Perl's hash implementation, they would work faster than methods we now must use to effectively accomplish the tasks suggested.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://217670]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2024-04-19 19:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found