in reply to Insert lines into specific stanza line

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

Replies are listed 'Best First'.
Re^2: Insert lines into specific stanza line
by xjlittle (Beadle) on Aug 13, 2010 at 20:13 UTC

    The second one is correct for what I am trying to accomplish. However I am getting the dreaded Use of uninitialized value in pattern match (m//)

    here is my code

    if ($change[$i] =~ /\b$ip\b/){ my $seen = 1; if ($seen && m{</stanza>}){ print $tl[0]; $seen = 0; }

    What am I missing?

      Please show enough code to establish context. I suspect that you're failing to define $ip (that's the only variable I see being used in a regex, and so the only place where you could have an uninitialized value in one), but since you've only showed this tiny snippet, there's no way for me to tell what else is wrong. There may also be other things wrong with your script, and I don't want to shoot in the dark.


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