in reply to Re^2: new to perl : how to match ?
in thread new to perl : how to match ?

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

Replies are listed 'Best First'.
Re^4: new to perl : how to match ?
by Sun (Initiate) on Oct 28, 2005 at 08:21 UTC
    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
      Okay, based on CB conversation - I think you want something like the following:
      #!/usr/bin/perl -w use strict; while (<DATA>) { next unless /data to/; chomp; my $server = (split)[2]; print "Server is $server\n"; if ($server eq "L650-F01") { # Do somthing } elsif ($server eq "MAP050-DFyutv01") { # Do something else } elsif ($server eq "gbsdfghfif7") { # Do something else else } } __DATA__ 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


      Note that there are much better ways to do this, such as using a hash to store all your server names in - but this is probably easier for you to understand.

      Hope this helps,
      --Darren :)
        hi What to do if i have some 100 servers' ?