in reply to Re: DateTime::Format::Flexible; for Log Parse with multiple formatted lines
in thread DateTime::Format::Flexible; for Log Parse with multiple formatted lines

What is the reason my original code will skip the last entry?

  • Comment on Re^2: DateTime::Format::Flexible; for Log Parse with multiple formatted lines

Replies are listed 'Best First'.
Re^3: DateTime::Format::Flexible; for Log Parse with multiple formatted lines
by huck (Prior) on Apr 04, 2017 at 14:35 UTC

    join only puts the first argument between array entries, not after each one

    $myfixedlog =~ /......(ENDOFLINE)/smg
    so there is no text ENDOFLINE after the last entry

Re^3: DateTime::Format::Flexible; for Log Parse with multiple formatted lines
by haukex (Archbishop) on Apr 04, 2017 at 14:33 UTC
    What is the reason my original code will skip the last entry?

    The problem is using join to join the lines using some string, which only inserts that string between elements of the array, and then using a regex that requires all entries to end on that string. In the following example, based on your original code, I'll demonstrate the problem, note how in the output, "Baz" is missing because it is not followed by "ENDOFLINE". The other problem I mentioned was that log entries that don't match the regex will be skipped (and may possibly even cause other entries to be parsed incorrectly, as this example shows):

    use warnings; use strict; use Data::Dumper; $Data::Dumper::Useqq=1; my @mylog = <DATA>; my $myfixedlog = join("ENDOFLINE", @mylog); print Dumper $myfixedlog; while ($myfixedlog =~ /([A-Za-z]+)\nENDOFLINE/smg) { print Dumper $1; } __DATA__ Foo 123 Bar Quz Baz

    Output:

    $VAR1 = "Foo\nENDOFLINE123\nENDOFLINEBar\nENDOFLINEQuz\nENDOFLINEBaz\n +"; $VAR1 = "Foo"; $VAR1 = "ENDOFLINEBar"; $VAR1 = "Quz";