Your immediate problem is that you read lines from a file with <IN> into the default variable and "write" them immediately into the tied object. However the tied object is essentially an array of lines. Because you haven't stripped the line end character from the input line you end up with two line ends on output.

However, there is a pile of other stuff that needs attention in your sample code. Consider this reworked version:

use strict; use warnings; use Tie::File; my @records; tie @records, 'Tie::File', "pile_out.txt"; my $in_file = "fake_vals.fem"; my $outIndex = 10; open my $in, '<', "$in_file" or die "cannot open '$in_file' for readin +g: $!\n"; while (my $line = <$in>) { next if $line !~ /\bNODE\b/i; chomp $line; $records[$outIndex] = $line; ++$outIndex; }

First off, always use strictures (use strict; use warnings; - see The strictures, according to Seuss).

Always use lexical file handles. In combination with strictures that will save some very embarrassing file messes.

Always use three parameter open. Usually at this point we suggest or die, but you are doing that already - great! However show the actual file path you were trying to open and the failure message. That will save a couple of iterations of debugging.

Avoid using the default variable. Using a manifest named variable helps with code readability and helps maintenance. It also avoids subtle problems with the default variable changing content when you don't expect it to.

A more contentions, but only marginally important suggestion is to use the pre increment operator unless logic demands the post increment operator. As a general rule stuff toward the end of a line gets lost and increment is important so do it up front.

Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond

In reply to Re: Newline's creep in, while using Tie::File by GrandFather
in thread 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.