venkatlab has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I am trying to match user input ($a=<STDIN>)with the data $_(read from a file). And print or store the data after the matched string.

For example:

if the User input is $a= temp

data read from a file is $_=temp validate end

then print result as Validate

if the 1st string matches then eliminate last string(end) & print the middle string.

sample Codenot working)

#!/usr/bin/perl -w $string = "The food is in the salad bar"; $a=<STDIN>; $string =~ m/($a)/; print "Before: $`\n"; print "Matched: $&\n"; print "After: $'\n";

Kindly help me

Thanks in Advance

Venky

20091208 Janitored by Corion: Added formatting, code tags, as per Writeup Formatting Tips

Replies are listed 'Best First'.
Re: Perl help needed:match user input with data read from a file
by keszler (Priest) on Dec 06, 2009 at 21:16 UTC
    The problem you have is that the $a=<STDIN> assignment includes the EOL character(s). For example, if you typed "salad", then hit the ENTER key, $a is set to "salad\n".

    Since there is no "\n" in $string, nothing you type can possibly match. Add chomp($a); to remove the "\n" from $a.

Re: Perl help needed:match user input with data read from a file
by GrandFather (Saint) on Dec 06, 2009 at 21:39 UTC

    As a matter of programming style you should always use strictures (use strict; use warnings;) and you should avoid $a and $b as variable names because they perform special magic with sort.


    True laziness is hard work