in reply to Re: Re: Conditonal insert into Array? (Tie::File)
in thread Conditonal insert into Array? (Tie::File)

If i'm interpreting your code and comments correctly, you are expecting that

$threads[$i] .= "\n<ul type=\"disc\">\n$link\n</ul>\n";

will insert 3 (or 4?) new lines after line $i. Whilst this may work, at least some of the time, it is TWWTDI (the wrong way to do it:).

From the Tie::File pod:

Inserting records that contain the record separator string is not supported by this module. It will probably produce a reasonable result, but what this result will be may change in a future version. Use 'splice' to insert records or to replace one record with several.

You should replace that line with

splice @threads, $i, 0, '<ul type=\"disc\">', "$link",'</ul>';

Which says 'insert the list (second line above) at position $i, whilst deleting nothing (0)'.

See perlfunc:splice for more details.


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller


Replies are listed 'Best First'.
Re: Re: Re: Re: Conditonal insert into Array? (Tie::File)
by u914 (Pilgrim) on May 28, 2003 at 22:18 UTC
    Ah! you are indeed correct!

    The way i'm doing it now *does* work (it's the last \n that's optional, not the first), but i'll try as the POD suggests..

    The important thing is that it's possible for the "parent" match to actually be the last line in the file (last element in the array) and so the array will have to be extended by n elements.

    thanks!