in reply to Re^2: Log Parsing
in thread Log Parsing

Could I for example:
open filehandle, >"/home/mj/newlogs"+a number+".log" print filehandle, $3 close filehandle
Not sure how to get the number in there...

Replies are listed 'Best First'.
Re^4: Log Parsing
by mattwortho (Acolyte) on Sep 07, 2007 at 12:40 UTC
    $count = 0; open filehandle, >"/home/mj/newlogs"+$count+".log" print filehandle, $3 close filehandle $count++;
    Something like that?? Thanks in advance.

      Variables interpolate directly in double-quoted strings, so you don't have to do anything extra for the filename. I prefer the three-argument form of open and error checking though:

      use strict; my $count = 0; my $filename = "/home/mj/newlogs$count.log"; open my $filehandle, ">", $filename or die "Couldn't create '$filename': $!"; ... loop print $filehandle $3; ... close $filehandle; $count++;

      The operator for string concatenation is the dot (.) - see perlop for the various operators.

      my $filename = "/home/mj/newlogs$count.log";

      is equivalent to

      my $filename = "/home/mj/newlogs" . $count . ".log";

      Please read print again to see what syntax I used for the print statement. Much of your code was not syntactically valid - for example, Perl wants a semicolon at the end of each statement. Reading perlsyn will help much.

      Also try to get in the habit of using strict and warnings to give Perl a chance to help you when you misspell variable names.