Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Getting matched word from String::Approx

by Athanasius (Archbishop)
on Dec 30, 2015 at 12:30 UTC ( [id://1151473]=note: print w/replies, xml ) Need Help??


in reply to Getting matched word from String::Approx

There could be more than one word matched, so try this:

#! perl use strict; use warnings; use String::Approx qw(amatch); my @list = qw(Apple Pear Orange); my @inputs = qw(aple appe peay range oranges mango appple aqqple); for my $word (@inputs) { my @matches = amatch($word, ['i'], @list); if (@matches) { print "Matched: $word to: ", join(', ', @matches), "\n"; } else { print "Unmatched: $word\n"; } }

Output:

22:27 >perl 1501_SoPW.pl Matched: aple to: Apple Matched: appe to: Apple Matched: peay to: Pear Matched: range to: Orange Matched: oranges to: Orange Unmatched: mango Matched: appple to: Apple Unmatched: aqqple 22:27 >

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^2: Getting matched word from String::Approx
by Anonymous Monk on Dec 30, 2015 at 12:44 UTC

    Many thanks Athanasius ))

    So amatch tries to match every word in @list even after one has been found? If the list is huge, would it be very slow?

    If we just wanted to get the first match from @list and return the result, is there a way to do that?

      If the list is huge, would it be very slow?

      Quite possibly. There’s a discussion in String::Approx#CONTROLLING-THE-CACHE about the tradeoffs between memory and running time.

      If we just wanted to get the first match from @list and return the result, is there a way to do that?

      From the documentation, this doesn’t seem to be possible, no. :-( When called in scalar context, the amatch function returns a single value, but that value is just true or false to indicate whether or not any matches were found.

      Note: Before committing to this module, you should read String::Approx#DESCRIPTION carefully and consider the alternatives. Depending on your requirements, a module such as Text::Levenshtein might suit you better.

      Hope that helps,

      Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

        Great thanks for your helpful advice and suggestions.

        I intended to use the module for a web-based search function for products. So if a user types a search string that is close to one of the products, then that product name will be suggested to the user. Since more than one match may be returned, I guess I will display the matched products as suggestions.

        Would that be considered appropriate in your opinion?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2024-04-18 10:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found