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
Solved

Searching for misspelled words and the line number

Posted on 2001-07-26
6
209 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
perl script 4 110
Search in text file in column and compare 4 53
ppm conversion to curl on a module install 8 85
Is it true to say that Python is successor of Perl? 7 189
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…
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…

809 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