Solved

Searching for misspelled words and the line number

Posted on 2001-07-26
6
210 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…

756 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