Click here to get back home

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

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
find a matching pattern in file and find it in another file too nani 03-13-2008
Posted by nani on March 13, 2008, 1:13 am
Please log in for more thread options
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";

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

Posted by comp.llang.perl.moderated on March 14, 2008, 1:20 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.
> ....
>
> > close $in or die "Cannot close $in\n";
>
> ^^^
> 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 :)


--
Charles DeRykus



Similar ThreadsPosted
Passing paramaters to the File::Find find() function February 15, 2006, 5:39 am
File::Find::find() is depth-first? January 10, 2005, 1:00 am
Stopping a File::Find on first find June 4, 2008, 6:22 pm
Help with Pattern matching. Matching multiple lines from while reading from a file. May 2, 2007, 11:37 pm
A do-file location: how the code inside that do-file find it? January 20, 2008, 12:32 am
multiline pattern matching from file January 22, 2007, 8:35 am
Using File::Find and only manipulating 1 file per directory. December 16, 2005, 10:53 am
File::Find losing one file? June 21, 2007, 2:23 pm
File::Find July 13, 2007, 2:52 pm
File::Find question. August 19, 2004, 7:31 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap