Reputation: 81
I am new to Perl, and I am writing a script to fetch some rows from a database:
my @rows = $conn->fetchrow_array(1,2,3);
the result will be three rows of single column.
12345
56789
12376
How should I join them together as 12345,56789,56789
I tried,
my $list = join ",", @rows.
Result: ARRAY(0x14f6de0),ARRAY(0x1508a90),ARRAY(0x15014c0)
Going through a foreach
loop just print the results with a new line:
12345
56789
12376
What am I doing wrong ? have I got the concept of fetchrow_array
wrong?
Upvotes: 2
Views: 788
Reputation: 118166
Each row is a reference to an array (because each row could contain multiple columns). Something like the following should work.
#!/usr/bin/perl
use strict; use warnings;
my @rows = (
[ 12345 ],
[ 56789 ],
[ 12376 ],
);
my @vals = map @$_, @rows;
print join(',', @vals), "\n";
However, you are better off using selectcol_arrayref
:
This utility method combines "prepare", "execute", and fetching one column from all the rows, into a single call. It returns a reference to an array containing the values of the first column from each row.
Upvotes: 6