Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Re^8: partial match between 2 files

by lakssreedhar (Acolyte)
on Dec 27, 2012 at 07:10 UTC ( #1010460=note: print w/replies, xml ) Need Help??

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

How can i do a partial match between a word inputted through STDIN and a list of words in a file and print the partial matched word from the file.

Replies are listed 'Best First'.
Re^9: partial match between 2 files
by Anonymous Monk on Dec 27, 2012 at 07:49 UTC

    How can i ...

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

      I need to print the word in the file which has the maximum partial match to the inputted 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 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,

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1010460]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2018-05-24 05:17 GMT
Find Nodes?
    Voting Booth?