stonecolddevin has asked for the wisdom of the Perl Monks concerning the following question:

hey all,

i'm trying to push the IDs of some records deleted in my MySQL database into an array and have it return something like, "records (numbers of each record deleted here) deleted".

any ideas as to how i could do this?

sub delete_forum { my $self = shift; my $q = $self->query; $self->dbh->do( q{ DELETE FROM mx_forums WHERE id=? }, {}, $_) for $q->param('forum'); # icky, i suppose, but it's just a hunch my @deleted; push @deleted, $_ for $q->param('forum'); return $frm->build_page( { title => "MX::FORUMS::ADD" , output => +"Forum " . join ',', @deleted . " deleted." } ); }
meh.

Replies are listed 'Best First'.
Re: manipulating data array into returning nice message
by GrandFather (Saint) on Feb 21, 2006 at 01:20 UTC

    It looks like the meat of your question could be answered thus:

    use warnings; use strict; my @deleted = ('Entry 1', 'Entry 2', 'Entry 3'); my $str = "Deleted forum"; @deleted > 1 ? ($str .= 's: ') : ($str .= ': '); $str .= join ', ', @deleted; print $str;

    Prints:

    Deleted forums: Entry 1, Entry 2, Entry 3

    DWIM is Perl's answer to Gödel
      GrandFather nailed it, however i'm having trouble pushing ALL the numbers into the array, it seems like it's just counting the number of elements in the array
      meh.
Re: manipulating data array into returning nice message
by rhesa (Vicar) on Feb 21, 2006 at 01:17 UTC
    you could just say:  my @deleted = $q->param('forum');.

    But, you don't know if each delete was successful, so you'd have to keep track of that too. In fact, that would give you the chance to count (and report) both the successful and the failed deletes.

    Something like this:

    my @to_delete = $q->param('forum'); my ( @failed, @deleted ); local $dbh->{RaiseError} = 1; # so we get exceptions for errors foreach my $id (@to_delete) { eval { $dbh->do( q{ delete from mx_forums where id=? }, {}, $id ); push @successful, $id; }; push @failed, $id if $@; } # ...