Your problem statement is weak and it is hard for me to see how your code could work. But it could be that something like this is what you want.
#!/usr/bin/perl -w use strict; my %add2stanza = ( '12.34.56.79' => "NEW DATA for 12.34.56.79", "12.34.56.80" => 'More new data for 12.34.56.80', ); while (<DATA>) { print; if (m/\<stanza\s+(.*)\>/) { my $number = $1; add_data($add2stanza{$number}) if $add2stanza{$number}; } } # not the best loop structure as the return is buried within # the loop but as a quick hack, shows some points about how # to do this... sub add_data { my $new_data = shift; while (<DATA>) { if (/\<\/stanza\>/) { print " $new_data\n"; print '</stanza>',"\n"; return; } else { print; } } } =prints <stanza 12.34.56.78> sl1 this is my line sl2 justanotherline </stanza> <line 1224> data </line> <stanza 12.34.56.79> sl1 this is my line sl2 justanotherline NEW DATA for 12.34.56.79 </stanza> <stanza 12.34.56.80> sl1 this is my line sl2 justanotherline More new data for 12.34.56.80 </stanza> <stanza 12.34.60.90> sl1 this is my line sl2 justanotherline </stanza> =cut __DATA__ <stanza 12.34.56.78> sl1 this is my line sl2 justanotherline </stanza> <line 1224> data </line> <stanza 12.34.56.79> sl1 this is my line sl2 justanotherline </stanza> <stanza 12.34.56.80> sl1 this is my line sl2 justanotherline </stanza> <stanza 12.34.60.90> sl1 this is my line sl2 justanotherline </stanza>

In reply to Re: Insert lines into specific stanza line by Marshall
in thread Insert lines into specific stanza line by xjlittle

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.