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

Last problem remaining is that everything is written to the last filehandler. I cant see why that is the case.

It looks to me like you are overwriting $ofh on each iteration of the first loop where you set up the array of handles. This would mean that every entry in your array points to the same file (the last file, of course). If you use a local variable inside that loop you may solve the problem. HTH.

Replies are listed 'Best First'.
Re^4: Write to multiple files according to multiple regex
by Foodeywo (Novice) on Jul 21, 2015 at 13:33 UTC

    thanks. how would that look like?

    I did something like this

    foreach(@inputs) { #localize the file glob, so FILE is unique to # the inner loop. local *FILE; local *OUTFILE; #NEW $file = "$FindBin::Bin/../rxo/$_"; $outfile = "$FindBin::Bin/../blocks/$_"; open(*FILE, "$file") || die; open(*OUTFILE, "> $outfile") || die; #NEW: added a star #push the typeglobe to the end of the array $fh = \*FILE; $ofh = \*OUTFILE; $regex = <$fh>; push(@regex,$regex); push(@filehandles,$ofh); }

    this results in a new error "print() on unopened filehandle OUTFILE"

Re^4: Write to multiple files according to multiple regex
by Foodeywo (Novice) on Jul 21, 2015 at 13:44 UTC
    hm. i probably solved it, not 100% sure.

    i did

    local *OUTFILE; open(OUTFILE, "> $outfile") || die; push(@filehandles,*OUTFILE);

    now the script writes to all files (but not the correct contents)