Short and Sweet, here's my conundrum:

I'm using Tie::File to open a file, and insert some text after a particular tag. I need to be able to make some decisions though, and in order to do that I need to know what the *next* element in the array is, and act accordingly.

Looking at my copy of the Perl Bookshelf, I'm not finding anything that applies, the closest thing being $#array. I've cribbed my technique from Dominus' treatise on Tie::File, but it's not an exactly parallel task.

Here's the pseudo-code i'm trying to work out:

### Update the main threads file tie @threads, 'Tie::File', $main_threads_file or syslog('err', "update +_threads couldn't open file: $main_threads_file"); if ($thread == $msg_id) { # put it right at the top, it's a new thread unshift @threads, $link; } else { for (@threads) { if (/$parent/) { if (##next element is "<ul>"##) { #insert $link after the next element (after the <ul>) } else { $_ .= "<ul>"; ## the next element after that .= $link ## the next element after *that* .= "</ul>" } last; } } } untie @threads;
The issue here is that i can't figure out how to say "The element after the current one" or "The second element after the current one".


In reply to Conditonal insert into Array? (Tie::File) by u914

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.