while (@array)

This will not do what you expect it to do (specifically, the loop will not terminate.) Please fix your code before posting it.

Based on the rest of your code, I assume that your stanza is contained in a file, and that your data is laid out just the way you specify - tags, etc. each on a line of its own. In that case, the answer is fairly simple:

#!/usr/bin/perl -w use strict; open my $config, '+<', 'stanza' or die "stanza: $!\n"; my @all = <$config>; seek $config, 0, 0; splice @all, -1, 0, "\tMy new data\n"; print $config @all; close $config;

This will insert the "\tMy new data\n" string just before the closing stanza.

Update: Re-read the OP's code, added another option for multiple stanzas in a file.

In case you need to select one of multiple stanzas by IP, here's a different version:

#!/usr/bin/perl -w use strict; open my $config, '+<', 'stanza' or die "stanza: $!\n"; my $ip = '12.34.56.78'; my $seen; while (<$config>){ if (m{^<stanza $ip>$}){ $seen = 1; } if ($seen && m{</stanza>}){ print "\tMy new data\n"; $seen = 0; } print; } close $config;

--
"Language shapes the way we think, and determines what we can think about."
-- B. L. Whorf

In reply to Re: Insert lines into specific stanza line by oko1
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.