find a matching pattern in file and find it in another file too

N

nani

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";
 
J

John W. Krahn

nani said:
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

use warnings;
use strict;
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";

You should include the $! variable in the error message so you know
*why* open failed.
#Check whether the file can be opened for reading

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

perldoc perlvar
[ SNIP ]
$PREMATCH
$‘ The string preceding whatever was matched by the last
successful pattern match (not counting any matches hidden
within a BLOCK or eval enclosed by the current BLOCK).
(Mnemonic: "‘" often precedes a quoted string.) This
variable is read-only.

The use of this variable anywhere in a program imposes a
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
considerable performance penalty on all regular expression
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
matches. See "BUGS".
^^^^^^^^^^^^^^^^^^^^^

$POSTMATCH
$’ The string following whatever was matched by the last
successful pattern match (not counting any matches hidden
within a BLOCK or eval() enclosed by the current BLOCK).
(Mnemonic: "’" often follows a quoted string.) Example:

local $_ = ’abcdefghi’;
/def/;
print "$‘:$&:$’\n"; # prints abc:def:ghi

This variable is read-only and dynamically scoped to the
current BLOCK.

The use of this variable anywhere in a program imposes a
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
considerable performance penalty on all regular expression
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
matches. See "BUGS".
^^^^^^^^^^^^^^^^^^^^^
&mysubroutine($x,$y);
}

sub mysubroutine
{

You are calling mysubroutine with the arguments $x and $y so the
contents of those two variables will be in the @_ array.
$a=$x;
$b=$y;

So why don't you get the contents of $a and $b from @_?

perldoc perlsub

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";
^^^
You are using the same filehandle that you used to open the other file
which means that the other file is now closed.

#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";};

perldoc -q "How do I match a pattern that is supplied by the user"

}
close $in or die "cannot close $out\n";
^^^ ^^^^

}


close $in or die "Cannot close $in\n";
^^^
Printing a filehandle in a string will not yield much useful information.



John
 
C

comp.llang.perl.moderated

^^^
Printing a filehandle in a string will not yield much useful information.

$! still needed but I always liked the old,
overloaded "name-n-handle" trick to save an
extra, newfangled lexical filehandle:

my $file = "/path/to/file";

open $file, '<', $file or die...
while (<$file>) {
...
}
close $file or die "can't close $file: $!";


I can imagine circumstances where you might not
be able to use the trick but I still like it :)
 

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top