in reply to Re: Handling multiple output files simultaneously using arrays of filehandles and filenames
in thread Handling multiple output files simultaneously using arrays of filehandles and filenames

Thanks - you are right that I want to open these files in order that I will under some logical conditions write a data line to them. One of my data items per line is indeed a $sector value and I will breakd down the data by $sector, outputting each line of input to the relevant output file per sector. My thinking was to use the following but it looks like you are advising me to insert curly braces which I will try although I don't really understand why this would make a difference and why print would have a problem sending a string to a file handler without curly braces...
$mydata=<INPUT>; while(<INPUT>){ #Handle data file line by line splitting it at every new line \n my(@data)=split/\n/,$_; #Now lets go through the whole input data file line by line for($i=0;$i<@data;$i++){ $mydata=$data[$i]; if($mydata=~/^Blah$some_regex(\d+)$some_more_regex_perhaps/){ #Get the sector value which is listed in each matching line of data $DataSector=$1; #Output line of matching data to output file for the relevant sector print $fh[$dataSector] "$mydata\n"; } }
  • Comment on Re^2: Handling multiple output files simultaneously using arrays of filehandles and filenames
  • Download Code

Replies are listed 'Best First'.
Re^3: Handling multiple output files simultaneously using arrays of filehandles and filenames
by choroba (Archbishop) on Jan 21, 2021 at 21:22 UTC
    > why print would have a problem sending a string to a file handler without curly braces

    You are conflating two levels: parsing of the source code and execution. When executed, print has no problem to send a string to a file handler, but there aren't any curlies anymore. The problem is the parser first needs to find out what and where to you want to print. And Perl parser is very simple and can never look ahead more than one token. Therefore, it can't check whether there's a comma after the $fh[$dataSector], because it only sees $fh and the next token is the index.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re^3: Handling multiple output files simultaneously using arrays of filehandles and filenames
by AnomalousMonk (Bishop) on Jan 21, 2021 at 23:21 UTC
    ... #Output line of matching data to output file for the relevant sector print $fh[$dataSector] "$mydata\n"; ...

    So the syntax you need to use is
        print { $fh[$dataSector] } "$mydata\n";
    (extra spacing around the curlies just for visibility/clarity).


    Give a man a fish:  <%-{-{-{-<

      Thanks again. I would like to confirm to you that this is indeed what I have done. Using curly braces allowed successful execution of the code. I also tried without the curly braces as a cross check of your correctness and I can confirm that the code failed in that case. Therefore I can confirm that on this occasion your advice was top notch! Spitze! Ausgezeichnet! Parfait! Fabuloso! Wunderbra!
        ... your advice was top notch! ... Wunderbra!

        I'm glad I've been able to provide so much support! :)


        Give a man a fish:  <%-{-{-{-<