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

Hello monks,
I have a question about iterating over the results from a Class::DBI object. The dataset that I'm expecting to return from my code does not match what I'm getting when I run the SQL code itself. I have two items, one that is a giftcard and one that is not, however, when I run through the loop it shows I have two gift cards. Here is my code:
my @line_rows = GiftCardLoad::DB::Lineitem->search(company => $self->{ +snum}, estimate_number => $est_obj->ESTIMATE_NUMBER); foreach (@line_rows) { print "ITEM NUMBER: " . $_->item_number . "\n"; if ($_->item_number eq 'GIFTCARD') { $verified++; } }
I've already tried turning the @line_rows into $line_rows and using while(my $line = $line_rows->next) {...} but that doesn't work either. Any help would be appreciated.
The SQL code I'm running to replicate this is SELECT * FROM LINEITEM WHERE COMPANY = '0696' AND ESTIMATE_NUMBER = '365578' This returns two rows of data, one with a giftcard and one without.

Replies are listed 'Best First'.
Re: Class::DBI result iteration
by blindluke (Hermit) on Oct 04, 2014 at 08:25 UTC

    Have you tried dumping the @line_rows?

    use Data::Dumper; my @line_rows = GiftCardLoad::DB::Lineitem->search(company => $self->{ +snum}, estimate_number => $est_obj->ESTIMATE_NUMBER); print Dumper \@line_rows;

    I assume that by saying "shows I have two gift cards", you mean to say that $verified is incremented two times in the loop you have shown. Have I understood you correctly?

    regards,
    Luke Jefferson