in reply to Re: Log Parsing
in thread Log Parsing

Sorry, I'm new to file handling in perl and wasn't sure how to create files and write to them. Not sure either how to make the new files' filenames because they'd have to be say the newfile + a number that is incrementing for example (they can't all be the same). Thanks in advance for your help. Matt.

Replies are listed 'Best First'.
Re^3: Log Parsing
by mattwortho (Acolyte) on Sep 07, 2007 at 12:37 UTC
    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...
      $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.