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

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)