Dear Monks,
This is the first time I am asking a question! I am trying to read a block of data (by matching a string) from one file and paste it in another file, at a specific line number. I have used the Tie::File option for the second file. My data in the first file (input file), is as follows:
NODE 32 0.00000 0.00000 -1.90000
NODE 33 0.00000 0.00000 -5.50000
NODE 34 0.00000 0.00000 -9.00000
NODE 35 0.00000 0.00000 -15.00000
NODE 36 0.00000 0.00000 -18.90000
NODE 37 0.00000 0.00000 -22.40000
NODE 38 0.00000 0.00000 -25.90000
NODE 39 0.00000 0.00000 -29.00000
NODE 40 0.00000 0.00000 -32.50000
NODE 41 0.00000 0.00000 -33.90000
NODE 42 0.00000 0.00000 -62.90000
BEAM 26 27 26 1 14 1
BEAM 27 28 27 1 13 1
BEAM 28 29 28 1 12 1
BEAM 29 30 29 1 11 1
BEAM 30 31 30 1 10 1
BEAM 31 32 31 1 9 1
BEAM 32 33 32 1 8 1
BEAM 33 34 33 1 7 1
BEAM 34 35 34 1 6 1
BEAM 35 36 35 1 5 1
BEAM 36 37 36 1 4 1
And the code I have written, is this:
use Tie::File;
my @records;
tie @records, 'Tie::File', "pile_out.txt";
# file containing data
$in_file = "fake_vals.fem";
# specific line number in the output file. The data from the input
# file should be written at this line.
$seed_line = 10;
open(IN,"$in_file") or die("cannot open the log file for reading\n");
# NODE is the string to be matched with, in the data from the
# input file
while(<IN>){######to read the input file
$records[$seed_line] = "$_" if($_=~/\bNODE\b/i);
$seed_line++;
}
close(IN);
The result I need is the following to be printed in the output file, starting at line no. 11 in the output file. All lines in the input file, starting with the string NODE should be printed in the output file, without any empty line in between.
NODE 32 0.00000 0.00000 -1.90000
NODE 33 0.00000 0.00000 -5.50000
NODE 34 0.00000 0.00000 -9.00000
NODE 35 0.00000 0.00000 -15.00000
NODE 36 0.00000 0.00000 -18.90000
NODE 37 0.00000 0.00000 -22.40000
NODE 38 0.00000 0.00000 -25.90000
NODE 39 0.00000 0.00000 -29.00000
NODE 40 0.00000 0.00000 -32.50000
NODE 41 0.00000 0.00000 -33.90000
NODE 42 0.00000 0.00000 -62.90000
However, my code is printing newlines after very single line of data, in the output file, as shown below.
NODE 32 0.00000 0.00000 -1.90000
NODE 33 0.00000 0.00000 -5.50000
NODE 34 0.00000 0.00000 -9.00000
NODE 35 0.00000 0.00000 -15.00000
NODE 36 0.00000 0.00000 -18.90000
NODE 37 0.00000 0.00000 -22.40000
NODE 38 0.00000 0.00000 -25.90000
NODE 39 0.00000 0.00000 -29.00000
NODE 40 0.00000 0.00000 -32.50000
NODE 41 0.00000 0.00000 -33.90000
NODE 42 0.00000 0.00000 -62.90000
Could you please tell me where I went wrong. This is the first time I have tried using Tie::File.