Perl String Search

Discussion in 'Perl Misc' started by TheDizzyDevil, Apr 19, 2006.

  1. 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++;
    }
     
    TheDizzyDevil, Apr 19, 2006
    #1
    1. Advertising

  2. TheDizzyDevil

    Guest

    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
     
    , Apr 19, 2006
    #2
    1. Advertising

  3. 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
     
    John W. Krahn, Apr 20, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Anand Pillai

    String search vs regexp search

    Anand Pillai, Oct 12, 2003, in forum: Python
    Replies:
    10
    Views:
    610
    Anand Pillai
    Oct 15, 2003
  2. Abby Lee
    Replies:
    5
    Views:
    446
    Abby Lee
    Aug 2, 2004
  3. walala
    Replies:
    21
    Views:
    224
    John W. Krahn
    Sep 23, 2003
  4. Ron M.
    Replies:
    4
    Views:
    148
    Tad McClellan
    Jan 5, 2004
  5. Claudio Palmeri

    Perl search string

    Claudio Palmeri, Feb 21, 2012, in forum: Perl Misc
    Replies:
    3
    Views:
    531
    Dr.Ruud
    Feb 22, 2012
Loading...

Share This Page