in reply to new to perl : how to match ?

The the problem is elsewhere in your code and/or logic. See the snippet below:

use strict; use warnings; my( @strings ) = ( 'data to gbsdff03dfif7 is', 'data to HR620-Asdf2', 'data to HP650dfgd-Af452 is' ); foreach my $line ( @strings ) { print "Matched $line.\n" if $line =~ /to (.*?) is/i; }

And the output is:

Matched data to gbsdff03dfif7 is. Matched data to HP650dfgd-Af452 is.

As you see, your regexp (which I cut and pasted along with your data) matches the first line and the third, not the middle line.


Dave

Replies are listed 'Best First'.
Re^2: new to perl : how to match ?
by Sun (Initiate) on Oct 28, 2005 at 06:40 UTC
    hi
    while (! eof inFile) { $line .= ' ' . <inFile>; chomp $line; next if ! ($line =~ /data\sto\s(.*?)\s+/i) and ! eof inFile;
    is the code and it is not working. any logic error ?

      Well, there are a few problems. Are you aware that $line is going to keep growing longer as you continue reading the file? So on the first iteration it contains one line of data, and on the second iteration, it contains the data from two lines of the file (but with the \n newline stripped away between the original file's lines). I'm not sure if that's really your intention or not.

      Next, you don't need all that ! eof inFILE stuff. In Perl, while( <filehandle> ) is magic enough for all that.

      Also, based on your current logic and on the fact that $line keeps getting new data appended to it, as soon as one line in the file matches (triggering the next if conditional), it will continue matching through the rest of the file, from that point forward.

      Do you really mean something more like this?

      while( my $line = <inFile> ) { chomp $line; next if $line !~ m/data\sto\s(.*?)\s*is/i; # ....the rest of the loop goes here... }

      Update:
      We really do wish to help, but the problem seems something of a moving target. Please tell us what you're trying to accomplish; I mean the bigger picture, as in "I want to iterate over the lines of a file and do this or do that..." Maybe if we knew what the objective was we could provide a little more meaningful insight.


      Dave

        hi david
        data to L650-F01 is ip address sdf sdf sdf sdf sdf sdfs Thu Oct 27 11:16:55 GMT 2005 data to L650-F01 is sdf..1 . . . sdf sdf sdf sdf sdf sdfs Thu Oct 27 11:16:55 GMT 2005 data to . . . #(no is here ) sdf gdfg dfgdf dfgdf dfgdf dfg Thu Oct 27 10:58:46 GMT 2005 data to MAP050-DFyutv01 is sdf gdfg dfgdf dfgdf dfgdf dfg Thu Oct 27 10:58:46 GMT 2005 data to . . . (same as above) sdf gdfg dfgdf dfgdf dfgdf dfg Thu Oct 27 10:58:46 GMT 2005 data to gbsdfghfif7 is ip address fgd dfgdf dfg dfg dfgd fdfgdf dfg ddfg fd dgfd Thu Oct 27 10:59:23 GMT 2005
        this the data file and i am checking for characters after "to".
        while (! eof inFile) { $line .= ' ' . <inFile>; chomp $line; next if ! ($line =~ /logging\sto\s+\s+(.*?)\s+/i) and ! eof inFile;
        is not checking for "data to . . . #(no is here ), data to gbsdfghfif7 is ip address" types.... i m confused on this. any clairifiactions pls let me know and i m working in it meanwhile