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.


In reply to Newline's creep in, while using Tie::File by always_coys

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.