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

Hi Monks!
I would like to ask if is possible, when one performs a MYSQL SELECT query that -at most- fetches 2 lines as result, to treat each line separately.
I know about:
my $ref; while($ref = $sth->fetchrow_arrayref) { print join (", ", @{$ref}), "\n"; }

but how can I only work with e.g. the 2nd result line and not the first? Thanks!
  • Comment on treat each result line individually in SELECT statement Perl-Mysql
  • Download Code

Replies are listed 'Best First'.
Re: treat each result line individually in SELECT statement Perl-Mysql
by hippo (Archbishop) on Oct 02, 2018 at 14:53 UTC
    my $ref; $sth->fetchrow_arrayref; # discard the first row while($ref = $sth->fetchrow_arrayref) { ...

    ... or use an OFFSET in your SQL.

Re: treat each result line individually in SELECT statement Perl-Mysql
by ForgotPasswordAgain (Vicar) on Oct 02, 2018 at 19:44 UTC

    If the number is really 2, and unless we're going for trivia points, I don't see the point of not just doing fetchall_arrayref and looking at the ->[-1] element. It's kinda unclear, since... do you only want to look at the 2nd element only if there are at least 2 or not? Or fall back to the last element?

    But, to make things interesting, I have -- rubs hands -- what I suspect is generally secret knowledge: there is a....hidden (ok, some might say undocumented, so you should avoid it :) function in DBD::mysql: DBD::mysql::st::dataseek($sth, $pos); ($pos is 0-based). I leave it to you how to abuse it, if you dare. :)

Re: treat each result line individually in SELECT statement Perl-Mysql
by cavac (Prior) on Oct 03, 2018 at 11:18 UTC

    Something like this?

    my $first = 1; while((my $ref = $sth->fetchrow_arrayref)) { if($first) { $first = 0; next; } print Dumper($ref), "\n"; }
    "For me, programming in Perl is like my cooking. The result may not always taste nice, but it's quick, painless and it get's food on the table."