Perl String Search

T

TheDizzyDevil

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++;
}
 
N

niall.macpherson

TheDizzyDevil said:
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
 
J

John W. Krahn

TheDizzyDevil said:
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;

Do you really need this variable?
print "What is your input file name:\n";
chomp($infile=<STDIN>);

chomp( my $infile = said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top