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

Discussion in 'Perl Misc' started by nani, Mar 13, 2008.

  1. nani

    nani Guest

    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";
    nani, Mar 13, 2008
    #1
    1. Advertising

  2. nani wrote:
    > 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
    --
    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
    John W. Krahn, Mar 13, 2008
    #2
    1. Advertising

  3. On Mar 13, 12:11 am, "John W. Krahn" <> wrote:
    > 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
    comp.llang.perl.moderated, Mar 14, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. vinitbhu
    Replies:
    4
    Views:
    388
    Mark Space
    Mar 17, 2008
  2. Sukhwinder Tambar

    Reading RTF + Text File and pattern matching

    Sukhwinder Tambar, Jan 12, 2009, in forum: Ruby
    Replies:
    2
    Views:
    144
    Sukhwinder Tambar
    Jan 15, 2009
  3. Marc Bissonnette

    Pattern matching : not matching problem

    Marc Bissonnette, Jan 8, 2004, in forum: Perl Misc
    Replies:
    9
    Views:
    221
    Marc Bissonnette
    Jan 13, 2004
  4. Bobby Chamness
    Replies:
    2
    Views:
    215
    Xicheng Jia
    May 3, 2007
  5. Replies:
    2
    Views:
    384
Loading...

Share This Page