Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Perl String Search

Reply
Thread Tools

Perl String Search

 
 
TheDizzyDevil
Guest
Posts: n/a
 
      04-19-2006
Hello everyone,

I am new to Perl and I am just feeling my way around. I was hoping if
someone could help me. I am trying to write a code that will search a
document and return a suffix, specified by the user, and have that
suffix in itallics. Below is the code I have thus far, but it does not
seem to work with out numerous line errors. Can some please help?

$b = 1;
print "What is your input file name:\n";
chomp($infile=<STDIN>);
open IN, $infile or die "No file, no fun!";
print "What is your output file name:\n";
chomp($outfile=<STDIN>);
print "What are you looking for:\n";
chomp($trag=<STDIN>);
open OUT, ">$outfile" or die "No file, no fun!";
while (<IN>) {
if (/$trag [?!'";: -]/) {
$word=$_;
$word=~ s/.+ (.+?$trag)[?!'";: -]/$1/gi;
$word=~ s/.+? ([n] +?$trag)[?!'";: -]/$1/gi;
$word=~ s/(.+?)$trag/$1<i>$trag<\/i>;
print OUT "$outfile $word: $_";
}
$b++;
}

 
Reply With Quote
 
 
 
 
niall.macpherson@ntlworld.com
Guest
Posts: n/a
 
      04-19-2006

TheDizzyDevil wrote:

> Hello everyone,
>
> I am new to Perl and I am just feeling my way around. I was hoping if
> someone could help me. I am trying to write a code that will search a
> document and return a suffix, specified by the user, and have that
> suffix in itallics. Below is the code I have thus far, but it does not
> seem to work with out numerous line errors. Can some please help?
>



Always get as much help as you can from perl - add

use strict;
use warnings;

Once you have done that , you will need to declare all your variables
using 'my'

When I did thjat and compiled I got

C:\develop\NiallPerlScripts>clpm13.pl
Substitution replacement not terminated at
C:\develop\NiallPerlScripts\clpm13.pl
line 15.

since this line

> $word=~ s/(.+?)$trag/$1<i>$trag<\/i>;


is missing a terminating slash - presumably you meant

$word=~ s/(.+?)$trag/$1<i>$trag<\/i>/;

I now have the following code

use strict;
use warnings;

my $b = 1;
print "What is your input file name:\n";
chomp(my $infile=<STDIN>);
open IN, $infile or die "No file, no fun!";
print "What is your output file name:\n";
chomp(my $outfile=<STDIN>);
print "What are you looking for:\n";
chomp(my $trag=<STDIN>);
open OUT, ">$outfile" or die "No file, no fun!";
while (<IN>) {
if (/$trag [?!'";: -]/) {
my $word=$_;
$word=~ s/.+ (.+?$trag)[?!'";: -]/$1/gi;
$word=~ s/.+? ([n] +?$trag)[?!'";: -]/$1/gi;
$word=~ s/(.+?)$trag/$1<i>$trag<\/i>;
print OUT "$outfile $word: $_";
}
$b++;
}

which compiles which your original version did not.

I can't really help you any further since I don't know what your sample
input looks like so I cannot tell whether this does what you want it to
do or not , but it does at least produce some output for me in this
format.

I gave it 'print' as the thing I was looking for , and a small perl
script as the input ansd I ended up with an output file containing the
following

C:\develop\NiallPerlScripts>more xxxxxxx
xxxxxxx <i>print</i> "\n1st = [$1]";
: print "\n1st = [$1]";
xxxxxxx <i>print</i> "\n2nd = [$2]\n";
: print "\n2nd = [$2]\n";
xxxxxxx <i>print</i> "\nNo match\n";
: print "\nNo match\n";

Hope this helps

 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      04-20-2006
TheDizzyDevil wrote:
>
> I am new to Perl and I am just feeling my way around. I was hoping if
> someone could help me. I am trying to write a code that will search a
> document and return a suffix, specified by the user, and have that
> suffix in itallics. Below is the code I have thus far, but it does not
> seem to work with out numerous line errors.


What *exactly* are the error messages?


use warnings;
use strict;

> $b = 1;


Do you really need this variable?

> print "What is your input file name:\n";
> chomp($infile=<STDIN>);


chomp( my $infile = <STDIN> );

> open IN, $infile or die "No file, no fun!";


You should include the $! variable in the error message so you know why it failed.

> print "What is your output file name:\n";
> chomp($outfile=<STDIN>);
> print "What are you looking for:\n";
> chomp($trag=<STDIN>);
> open OUT, ">$outfile" or die "No file, no fun!";
> while (<IN>) {
> if (/$trag [?!'";: -]/) {

^
You are matching the contents of the $trag variable followed by a space
character followed by the character class. You should probably use the /o option.

> $word=$_;
> $word=~ s/.+ (.+?$trag)[?!'";: -]/$1/gi;

^^
> $word=~ s/.+? ([n] +?$trag)[?!'";: -]/$1/gi;

^^
These patterns do not include a space character? Why use the /i option now
when the original did not?

> $word=~ s/(.+?)$trag/$1<i>$trag<\/i>;


You are missing the terminating '/' delimiter.

> print OUT "$outfile $word: $_";
> }
> $b++;
> }


You may need to use quotemeta because $trag may contain some characters that
are special in regular expressions.



John
--
use Perl;
program
fulfillment
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
| SEO , Search Engine Optimizer, SEARCH OPtiMIzAtIoN with SeaRch OPtiMizer optimizer.seo@gmail.com Digital Photography 0 04-22-2007 04:20 AM
How to search for literal string in Windows Desktop Search? yong321@yahoo.com Computer Support 0 02-06-2007 04:58 AM
search within a search within a search - looking for better way...my script times out Abby Lee ASP General 5 08-02-2004 04:01 PM
String search vs regexp search Anand Pillai Python 10 10-15-2003 08:21 AM
some stupid questions about string search & replace in perl walala Perl Misc 21 09-23-2003 06:29 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57