Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

treat each result line individually in SELECT statement Perl-Mysql

by Anonymous Monk
on Oct 02, 2018 at 13:31 UTC ( [id://1223400]=perlquestion: print w/replies, xml ) Need Help??

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 (Bishop) 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 (Priest) 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 (Parson) 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."

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1223400]
Approved by Eily
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2024-04-19 14:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found