Well, that is not exactly how i would have solved the problem, but if it works for you, then good. If you are curious as to how i would solve the problem, then read on.

Use A Database!

That's right. Store your data in place that is used to store data. Display your data in a place that is used to display data. Don't mix the two. Repeat. Don't mix content and presentation. You say don't have access to a database? Get one! Between SQLite, MySQL, and PostgreSQL you have no excuses not to "upgrade" your skills. ;)

Of course, seeing as how you have come along this far with your solution, switching now probably seems impossible. I understand that completely, but here is some code that keeps track of "threads" via DBI.pm and CGI.pm. I used the MySQL database with the following table creation syntax:

CREATE TABLE node ( id int(10) NOT NULL auto_increment, name varchar(64) NOT NULL default '', parent int(10) default '0', PRIMARY KEY (id) );
The following CGI script will print a form allowing the user to enter a title for the new "message" and an optional "parent id" to link to. It's id is auto-generated by the database and the user can't change it. Any messages are printed out via a recursive subroutine, but first they are fetched from the database and placed into a datastructure whose structure was concocted by me in a rather quick and dirty fashion. Improvements are always welcome.
use strict; use warnings; use DBI; use Data::Dumper; use CGI::Pretty qw(:standard *ul); my %node; my $dbh = DBI->connect( qw(DBI:vendor:database:host user pass), {RaiseError=>1} ); print header(),start_html('messages'), h1('messages'),start_form, 'New thread title: ', textfield('name'),br, 'Parent thread: ', textfield('parent'),br, submit('go'),end_form, ; insert_message(param('name'),param('parent')) if param('go'); display_messages(); print end_html; $dbh->disconnect; sub display_messages { my $sth = $dbh->prepare('select id,name,parent from node'); $sth->execute; while (my $row = $sth->fetchrow_hashref()) { $node{$row->{id}} = { name => $row->{name} }; push @{$node{$row->{parent}}->{children}}, $row->{id} if $row->{parent}; } # uncomment this to see the datastructure #print pre(Dumper \%node),hr; print start_ul; display_thread($_) for sort {$a<=>$b} keys %node; print end_ul; } sub display_thread { my $id = shift; my $node = delete $node{$id}; return unless $node; print li("$id: " . $node->{name}); return unless $node->{children}; print start_ul; display_thread($_) for @{$node->{children}}; print end_ul; } sub insert_message { my ($name,$parent) = @_; my $sth = $dbh->prepare(' insert into node(name,parent) values(?,?) '); $sth->execute($name,$parent); } # this sub is not used, but i'll leave it anyway ;) sub get_last_id { return $dbh->selectcol_arrayref('SELECT last_insert_id()')->[0]; }

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

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