Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

word finder

by fxmakers (Friar)
on Apr 12, 2004 at 12:50 UTC ( #344370=perlquestion: print w/replies, xml ) Need Help??

fxmakers has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: word finder
by Fletch (Bishop) on Apr 12, 2004 at 12:57 UTC
    perl -ne 'print if /\bfoo\b/' in.txt

    If that's not it, start with a better problem statement.

Re: word finder
by halley (Prior) on Apr 12, 2004 at 12:59 UTC
    You almost surely don't have to think about this problem in terms of chr() (unless you really wanted to). Perl simplifies this task so much that I was initially not sure how to respond. Your question is incomplete. What data are you searching? What did you want to do with it? What are the additional rules for what you call a "word"?
    C:\stuff> perl -ne "print if /\b(foo)\b/" < input.txt > matching.tx +t

    --
    [ e d @ h a l l e y . c c ]

      Well, I'd like to solve a game like a MasterMind, or a Trivia game, or find a sort of password, looping every possible key word (not only the dictionnary words, a random one for ex.)


      UPDATED:
      After some brainstorming I got my solution.
      Here's my script if anyone is interested:

      #!/usr/bin/perl -w use strict; my $length = 4; # max length of the word to find my $find = "test"; # the word to find sub gen_word { my $prev = shift; my $len = shift; if ($len == 1) { for (my $idx = 65; $idx <= 122; $idx++) { my $word = $prev . chr($idx); print "word: $word\n"; if ($find eq $word) { print "found: " . $word . "\n"; exit(); } } } else { for (my $idx = 65; $idx <= 122; $idx++) { &gen_word($prev . chr($i +dx), $len - 1); } } } &gen_word("", $length); 1;
        I still think your question (and maybe your quest) is a bit too vague for us to help much. Be crisp. Decide on one thing you want to get done, and be as clear and specific as possible about your objective, and we can help with the strategy and tactics.

        For example, cycling through all possible "words" is one algorithm. You have to be specific about what constitutes a "word." In MasterMind, you choose a known number of colored pegs in sequence. Passwords have far more letters and symbols which can appear in each position, and you don't even know the length. In a trivia game, your answer "word" might be something like "the Battle of Bull Run" which is a pretty long word to guess if you're trying every possible permutation including common punctuation.

        Once you decide what a "word" is, you can write a program to cycle through all possible permutations pretty easily, even if it takes the computer several years to run that program.

        Your other part of the problem is to decide if a given guess "word" is the correct answer, or even is a word worth trying. MasterMind only gives you a handful of tries before you lose, so you have to pay attention to the feedback. A login prompt might lock the account after three tries without giving any feedback at all. And trivia game show hosts rarely let you try more than one answer and only give you a few seconds to think about it.

        --
        [ e d @ h a l l e y . c c ]

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (2)
As of 2022-11-27 01:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?