in reply to Re: Write to multiple files according to multiple regex
in thread Write to multiple files according to multiple regex

Thanks! Refering explicitly to $line is the key it seems, since within foreach, print refers to the elements of @inputs by default.

I also added $line=~ to make it work. Curly braces (# 6.) where also neccessary.

Now it writes all matches, but it puts all matches into the last filehandler only, throwing

"Use of uninitialized value $_ in pattern match (m//) at parser.pl line 60, <> line 4401."

for every line

  • Comment on Re^2: Write to multiple files according to multiple regex

Replies are listed 'Best First'.
Re^3: Write to multiple files according to multiple regex
by Monk::Thomas (Friar) on Jul 21, 2015 at 12:53 UTC

    The conditional to print looked rather fishy, but I forgot about it. Try:

    print {$filehandles[$i]} $line - if (/$regex[$i]/../^END_OF_BLOCK/); + if ($line =~ /$regex[$i]/../^END_OF_BLOCK/);

    or did you already do exactly that? Please show your updated code.

      yes I added the $line=~
      print {$filehandles[$i]} $line if $line =~ /$regex[$i]/;
      I dropped the ../^END/ to play with the $/ operator. Not working yet though. Before that I had
      print {$filehandles[$i]} $line if ($line =~ /$regex[$i]/../^END/);
      which redundantly wrote all (correct) lines to the last (not the correct) filehandler.