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

In DBI.pm,
while (($blah,$blah,$blah) = $cursor->fetchrow){..}
I have an array of an array (multiple lines of split() information) that I would like to return in such a fashion that fetchrow does (in OOP). Is this just a variable incrementing that returns the next line after each call? I'd hope there would be a more elegant solution you brilliant people could help me with. Thanks

Replies are listed 'Best First'.
Re: Returning Array of Arrays (OOP)
by Fastolfe (Vicar) on Dec 30, 2000 at 06:54 UTC
    If you already have all of the data in memory it doesn't do much good to keep passing it out piecemeal like that. If you were reading from a file, you could use your function to read a single line and return an array of each split piece of that line in a fashion similer to fetchrow. If you really do want to pass out pieces of a pre-existing array like that, though, you can do it like this:
    my @array = ([1, 2, 3], [4, 5, 6], [7, 8, 9]); # non-destructive my $index = 0; sub nextitem { return if $index > $#array; return @{$array[$index++]}; } # more elegant, but destroys @array sub nextitem { return unless @array; return @{shift(@array)}; }
    Is this what you meant?
Re: Returning Array of Arrays (OOP)
by runrig (Abbot) on Dec 30, 2000 at 11:32 UTC
    Perhaps IO::ScalarArray is close to what you're looking for?

    It would be similar to the effect of fetchrow_arrayref. In order to simulate fetchrow_array, you'd have to dereference the array reference as each 'line' is 'read'.