Solved

Searching for misspelled words and the line number

Posted on 2001-07-26
6
208 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
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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

813 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now