This is in relation to 325952.

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>


In reply to rebuilding a hash (more) by coldfingertips

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.