copy the which is before ",(comma)" in one file and matching pattern in another file

Problem Def:  copy the pattern which before ",(comma)" and find a
matching pattern in another file.

status: i wrote following code. but it is not working properly. plz
help me.

#! C:\Perl\bin\perl.exe

print "hello\n";

print "Please Enter Input File name(Give the complete path):";
$infile=<STDIN>;  #give the input file name here
chomp($infile);
open ($in, "<", $infile) or die "Cannot open file for reading\n";
#Check whether the file can be opened for reading

while (<$in>)
{
if(/,/) {print "before match: $`\t and after match: $'\n\n";};
$x=$';
$y=$`;
&mysubroutine($x,$y);

}

        sub mysubroutine
        {
        $a=$x;
        $b=$y;
        print "Please Enter the Output File name (Give the compelte
path):";
        $infile=<STDIN>;    #give the output file name here
        chomp($infile);
        open ($in, "<", $infile) or die "cannot open file to write\n";
#Check whether the file can be opened for writing

        print "$b\n";
        while (<$in>)
        {
        print "pattern to find: $y\n";
        if (/$b/) {print "Cheers@\n";};
        }
        close $in or die "cannot close $out\n";
        }

close $in or die "Cannot close $in\n";

0
gkreddybh
3/13/2008 5:29:41 AM
📁 perl.beginners
📃 29226 articles.
⭐ 3 followers.

💬 1 Replies
👁️‍🗨️ 228 Views

You want to start the code with these:
use warnings;
use strict;

> if(/,/) {print "before match: $`\t and after match: $'\n\n";};
> $x=$';
> $y=$`;
> &mysubroutine($x,$y);
Should be:
if(/,/) {
  print "before match: $`\t and after match: $'\n\n";
  $x=$';
  $y=$`;
  mysubroutine($x,$y);
}
No ';' at the end of the if block. Drop the & before the sub call.
Or ever do this:
if(/,/) {
  print "before match: $`\t and after match: $'\n\n";
  mysubroutine($', $`);
}

But what's really broken is this (in the sub):
>       $a=$x;
>       $b=$y;
Should be:
my ($a, $b) = @_;
I /think/ $x, $y are not defined in the sub.
So this is wrong, too:
>       print "pattern to find: $y\n";
>       if (/$b/) {print "Cheers@\n";};
No $y defined here. Stick with $b
0
yitzle
3/13/2008 3:06:11 PM
Reply:

Similar Posts: