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

Re: searching problem

by choroba (Cardinal)
on Jan 18, 2013 at 10:27 UTC ( [id://1014024]=note: print w/replies, xml ) Need Help??


in reply to searching problem

You can create a regex that matches any of the reversed words:
#!/usr/bin/perl use warnings; use strict; my @words = qw(mamy dady bal foo bar ymamaa arabic); my $regex = join '|', map scalar reverse, @words; $regex = qr/$regex/; print "$_\n" for grep /$regex/, @words;
لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Replies are listed 'Best First'.
Re^2: searching problem
by BillKSmith (Monsignor) on Jan 18, 2013 at 13:45 UTC
    A small simplification:
    my $regex = reverse join '|', @words;
    Bill
Re^2: searching problem
by Lotus1 (Vicar) on Jan 18, 2013 at 22:57 UTC

    The way I understood the OP was print out the word which when in reverse matches any other word. Your solution does the opposite. For example instead of printing bar since 'rab' matches in arabic, it prints arabic. ymamaa was also printed since 'mamy' in reverse matches it. My point is that arabic in reverse doesn't match anything.

    #!/usr/bin/perl use warnings; use strict; my @words = qw(mamy dady bal foo bar xrabbit ymamaa arabic); my $list = join ' ', @words; foreach( @words ) { my $rev = reverse $_; print "$_\n" if $list =~ /$rev/; } __END__ mamy bar

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-03-19 07:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found