coldfingertips has asked for the wisdom of the Perl Monks concerning the following question:
Still having problems and I have made a few improvements.
What this snippet is doing is tying a hash to my database %upload which contains image information. When images are uploaded to my gallery, I give them $key++ and I compare key names to print them in the correct order.
Now I have a delete function on my script where I delete a key to remove all the images and it's information from my database. It seems to work fine if I delete most images, everything else works. But when I delete the newest image (no matter what number it is, it seems to delete more than one image and decides to remove a few other ones.
In short, when images are uploaded through a form I do a num++ on their key. This means I have keys 0-how many ever images I have at the time. When I delete a key that's somewhere in between, let's say I want to delete key 14 when I have 20 total.. I need it to rewrite the database so there are keys 0-19 without a gap in key numbers and have all the data still be accurate.
Hard to explain why I need to remove the gap, but it's something I need it to do. Can anyone see my logic mistake?
Thank you for your help!
sub processing { my %upload; my $upload = "gal.db"; tie %upload, "DB_File", "$gal", O_CREAT | O_RDWR, 0644, $DB_BTREE or die "Cannot open file 'gal': $!\n"; ### # Setup our params ### my $edit = url_param('edit'); my $del = url_param('rem'); #################### # Check for DELETE call #################### if ($del ne "") { if (exists $upload{$del}) { my ( $filename, $title, $comments, $width, $height, $count ) = spl +it ( /::/, $upload{$del} ); ### # Rewriting $filename to a .png ### my $thumbs = $filename; $thumbs =~ m/([a-zA-Z0-9]+)\.()/; $thumbs = "$1.png"; ### # Deleting files from both image folders ### delete $upload{$del}; unlink "$fullimagedir/$filename" or die "Cannot delete image: $!"; unlink "$fullthumbsdir/$thumbs" or die "Cannot delete image: $!"; print "<center><font color=blue>Image removed.</font></center>"; my $num = "-1"; foreach (sort keys %upload) { $num++; my ( $filename2, $title2, $comments2, $width2, $height2, $count2 +) = split ( /::/, $upload{$_} ); my $joined = join("::", $filename2, $title2, $comments2, $width2, + $height2, $count2); $upload{$num} = $joined; print "$num = $joined<br>"; } my $count = "-1"; foreach (sort keys %upload) { $count++; } print "$count"; delete $upload{$count}; } else { print "<center><font color=red>ERROR! Image does not exist.</fon +t></center>"; }
update (broquaint): added <readmore>
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: rebuilding a hash (more)
by ysth (Canon) on Feb 20, 2004 at 07:58 UTC | |
by coldfingertips (Pilgrim) on Feb 20, 2004 at 10:26 UTC | |
by ysth (Canon) on Feb 20, 2004 at 16:05 UTC | |
|
Re: rebuilding a hash (more)
by Skeeve (Parson) on Feb 20, 2004 at 06:51 UTC | |
by davido (Cardinal) on Feb 20, 2004 at 06:55 UTC | |
by Skeeve (Parson) on Feb 20, 2004 at 08:11 UTC | |
by coldfingertips (Pilgrim) on Feb 20, 2004 at 07:16 UTC | |
|
Re: rebuilding a hash (more)
by tbone1 (Monsignor) on Feb 20, 2004 at 13:36 UTC |