Solved

Searching for misspelled words and the line number

Posted on 2001-07-26
6
212 Views
Last Modified: 2012-06-21
I'm trying to write a script that searches for specific misspelled words.  For example, to find any misspelling for the word ISSN at the beginning of a line, I started doing this:


#! /usr/bin/perl -n

/^I(S|[^S])[^S]N/ && print;
/^I(S|[^S])[^S]N/ && print;
/^I[^S]([S]|[^S])[^N]/ && print;

The first line looks for possible misspelling in the 2nd & 3rd letters, the second line looks for possible misspelling in the 2nd letter, etc., etc.

This is a nightmare and I have about 70 such key words; is there an easier way?

If the line number that the error appears can be printed also would be great too.




0
Comment
Question by:hadrons
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 51

Expert Comment

by:ahoffmann
ID: 6325874
write the correct words in a array, like
   @ok=('ISSN', 'etc.');
then match the first word against the array:
   if (!grep(/^$firstword$/,@ok) ) { print "missmatch\n"; }
0
 
LVL 84

Accepted Solution

by:
ozo earned 200 total points
ID: 6327271
Faster to write the correct words in a hash, like
  @ok{'ISSN', 'etc.'}=();
then
  if( !exists $ok{$firstword} ){ print "missmatch\n"; }

Or,
   use String::Approx;
0
 

Author Comment

by:hadrons
ID: 6329736
You guys are going to think I'm so clueless, but I wasn't able to get the desired results.  I'm sure it me not setting this up corrected, so I'll go thru it blow by blow.

I used a file with these lines:

ISSN: right1
ISBN: wrong1
NAME: right1
NAEM: wrong1

created the command script:

#! /usr/bin/perl
@ok=('ISSN', 'NAME');
if (!grep(/^$firstword$/,@ok) ) { print "missmatch\n"; }

it printed just missmatch; and when I edited the file with just the correct names, it did the same thing.


Afterwards, I looked at this line: if (!grep(/^$firstword$/,@ok) ) { print "missmatch\n"; }

and changed it to: if (!grep(/^ISSN/,@ok) ) { print "missmatch\n"; }

And it compiled, but nothing occurred.  I did the same thing to ozo code and the results matched (so this is why I think the problem is with the way I'm setting it up)





0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 8

Expert Comment

by:bebonham
ID: 6332493
I don't know grep, but here is the hash version
###this simulates readin from file###
$arr[0]="ISSN: right1";
$arr[1]="ISBN: wrong1";
$arr[2]="NAME: right1";
$arr[3]="NAEM: wrong1";
###if you want to readin from file you can use
###@arr=<FILE>;

@ok{'ISSN', 'NAME'}=();
foreach $firstword(@arr)
{
$firstword=~s/^([^:]+):.*/\1/;
if(!exists $ok{$firstword} ){ print  $firstword . " missmatch\n"; }
}
0
 
LVL 8

Expert Comment

by:bebonham
ID: 6332495
I was just fleshing out ozo's solution, he already provided all the code.
0
 

Author Comment

by:hadrons
ID: 6391138
sorry for the long delay; there was a medical emergency and I was away from everything.  It was String::Approx; that did the trick for me, it was new to me to use modules, but I liked the results.

And I was able to use parts of the code from all of you to improve other pieces of code I was using.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

622 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question