|
Posted by John W. Krahn on March 13, 2008, 3:11 am
Please log in for more thread options
nani wrote:
> Problem Def: copy the pattern which before ",(comma)" and find a
> matching pattern in another file.
>=20
> status: i wrote following code. but it is not working properly. plz
> help me.
>=20
>=20
> #! C:\Perl\bin\perl.exe
use warnings;
use strict;
> print "hello\n";
>=20
> print "Please Enter Input File name(Give the complete path):";
> $infile=3D<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=20
*why* open failed.
> #Check whether the file can be opened for reading
>=20
> while (<$in>)
> {
> if(/,/) {print "before match: $`\t and after match: $'\n\n";};
> $x=3D$';
> $y=3D$`;
perldoc perlvar
[ SNIP ]
$PREMATCH
$=91 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: "=91" 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
$=92 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: "=92" often follows a quoted string.) Example:
local $_ =3D =92abcdefghi=92;
/def/;
print "$=91:$&:$=92\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);
> }
>=20
> sub mysubroutine
> {
You are calling mysubroutine with the arguments $x and $y so the=20
contents of those two variables will be in the @_ array.
> $a=3D$x;
> $b=3D$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=3D<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=20
which means that the other file is now closed.
> #Check whether the file can be opened for writing
>=20
> 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";
^^^ ^^^^
> }
>=20
>=20
> close $in or die "Cannot close $in\n";
^^^
Printing a filehandle in a string will not yield much useful information.=
John
--=20
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
|