Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^9: partial match between 2 files

by Anonymous Monk
on Dec 27, 2012 at 07:49 UTC ( #1010464=note: print w/ replies, xml ) Need Help??


in reply to Re^8: partial match between 2 files
in thread partial match between 2 files

How can i ...

Write some code? STDIN is just a filehandle, readline from it


Comment on Re^9: partial match between 2 files
Re^10: partial match between 2 files
by lakssreedhar (Acolyte) on Dec 27, 2012 at 08:42 UTC

    I need to print the word in the file which has the maximum partial match to the inputted string.ie if the inputted string is fallen and the file has words fal,fall,falle,the value to be printed is falle.The code below gives errors

    #!/usr/bin/perl-w use warnings; use strict; open my $fh1, '<', 'words' or die $!; my $a=$_; while (my $f1 = <$fh1> ) { chomp $f1; my $f2 = $a; chomp $f2; if($f2=~m/$f1/i){print"$f1\n";} }

      The code below gives errors

      Maybe you should figure out what they mean :/ use diagnostics

      The line my$a=$_; does nothing useful, since $_ is uninitialised. As Anonymous Monk says, you need to read from STDIN. For example:

      #! perl use strict; use warnings; print "Enter the target word: "; chomp(my $target = <STDIN>); my $in_file = 'words.txt'; open(my $in, '<', $in_file) or die "Cannot open file '$in_file' for re +ading: $!"; my @matches; while (<$in>) { chomp; push @matches, $_ if $target =~ /$_/i; } close $in or die "Cannot close file '$in_file': $!"; @matches = sort { length $a <=> length $b } @matches; print "The closest match is: ", $matches[-1], "\n";

      If the file “words.txt” contains:

      fal falle fall

      then, when “fallen” is entered from the keyboard, the output of the above script is:

      2:31 >perl 454_SoPW.pl Enter the target word: fallen The closest match is: falle 2:31 >

      Hope that helps,

      Update: Fixed error in sort: changed > to <=>. Also changed the order of words in the input file.

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

        The code produces an error "Use of uninitialized value in print" when a word awiSayanA is compared with 2 words
        awiSayaM
        awiSayanZ
        present in dictionary.Output is not produced.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (8)
As of 2014-12-19 23:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (94 votes), past polls