in reply to Re: Conditonal insert into Array? (Tie::File)
in thread Conditonal insert into Array? (Tie::File)
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.
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...#!/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;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
(jeffa) 3Re: Conditonal insert into Array? (Tie::File)
by jeffa (Bishop) on May 29, 2003 at 06:30 UTC | |
|
Re: Re: Re: Conditonal insert into Array? (Tie::File)
by BrowserUk (Patriarch) on May 28, 2003 at 15:12 UTC | |
by u914 (Pilgrim) on May 28, 2003 at 22:18 UTC |