Test Script

As requested, i've dummied up a short test script to show the functionality of the above snippet...

The instructions are embedded as comments (i know, should be POD..) in the script, and it needs no inputs beyond fiddling with the indicated 3 variables between runs.

$msg_id should be incremented with each running of the script.
$thread should be the last $msg_id, plus one to make a new top-level post, or it should inherit the $thread of the parent if a reply
$parent can be set to the $thread_$msg_id of any arbitrary existing post to create a reply.

When replying, the $thread should always match the parent's $thread, but the $msg_id is the current $msg_id, in the incremental sequence. In real life all of this is accomplished by simply writing a number to a file, and incrementing it each time the script is called.

It will produce it's own output on first running, and should be allowed to continue to work on that file for subsequent runs. The output is best understood if viewed with a web browser.

In real life, this output file is inserted into another .html file via SSI, and is wrapped in <ol> </ol> tags, which will number all of the top-level posts. Try it manually and you'll see what i mean.

#!/usr/bin/perl -w use Tie::File; use strict; ### ### This is a complete, self-contained dummy script to ### test the insert functionality of this sub... ### ###################################################################### +#### ### ### These vars should be changed to demonstrate different behaviours ### my $thread = "208"; # is the thread identifier my $msg_id = "211"; # is this message's unique number my $parent = "208_208"; # is the message_id of the parent post ## To create new top-level post set $thread=$msg_id, $parent is ununse +d ## and should be undef ## All replies to given top-level post should carry same $thread value +, ## whereas $msg_id is incremented ## Set $parent to: ($thread . "_" . $msg_id) to make a reply to the ## post which carries that message_id string in the html comment ## $msg_id should be unique for each post ($msg_id++) ## $thread should be unique for each thread ###################################################################### +### ### ### These vars are (relatively) static ### my @threads; # the array we will use my $main_threads_file = "threads.html"; # the threads file we will +work on my $link = "<li><!-- message_id=$thread" . "_" . "$msg_id --><a href= +\"http://link_to_post\">dummy link to message $thread - $msg_id</a>"; ### 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 my $i (0 .. $#threads) { if ($threads[$i] =~ $parent) { if ($threads[$i+1] eq "<ul type=\"disc\">") { # insert $link after the next element (after the <ul>) $threads[$i+1] .= "\n$link"; } else { # insert three new lines/records after the parent $threads[$i] .= "\n<ul type=\"disc\">\n$link\n</ul>\n" +; } last; } } } untie @threads;
This isn't really the final state of the production code for this, as it also needs to understand that sometimes replies should not be listed on the main threads file, but rather only the total *number* of replies, and users must click on the parent link to expand the thread. If anyone's interested i can post the complete code including the flat-threads-file aware stuff...

In reply to Re: Re: Conditonal insert into Array? (Tie::File) by u914
in thread 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.