Re: Convenience function selectall_hasharrayref
Tim Bunce wrote: On Tue, Jan 10, 2006 at 12:47:54PM +0100, Peter J. Holzer wrote: or selectall_arrayhashref? Anyway, I rather frequently find that the most natural way to represent a query result is an array of hashes: Each row is hashref, but the the rows are in an array(ref) so that the order is preserved, and the columns can be accessed by name. Proposed use: my $emp = $dbh-selectall_hasharrayref(select * from emp order by ename); for (@$emp) { print $_-{ename} ($_-{empno}): $_-{job}\n; } or for ($first .. $last) { print $emp-[$_]{ename}, \n; } or something like that. What do you think? Anything wrong with the existing my $emp = $dbh-selectall_arrayref(..., { Slice = {} }); ? Note that selectall_*arrayref* defines the outermost data that the method returns. What's inside that is controlled by attributes. For example, want an array or arrays where the inner arrays contain only the 2nd and 3rd fields? Easy: my $emp = $dbh-selectall_arrayref(..., { Slice = [2,3] }); Please spread the word. Way too many people seem to not know about this! Tim. Maybe if there would be an example like this in the POD people would realize this? I'd volunteer to create such a doc-patch but where can I get the latest source with all changes made since the last cpan release? Tom
Re: Convenience function selectall_hasharrayref
On Wed, Jan 11, 2006 at 10:37:58AM +0100, Tom Schindl wrote: Tim Bunce wrote: Anything wrong with the existing my $emp = $dbh-selectall_arrayref(..., { Slice = {} }); ? Please spread the word. Way too many people seem to not know about this! Maybe if there would be an example like this in the POD people would realize this? I'd volunteer to create such a doc-patch Thanks! but where can I get the latest source with all changes made since the last cpan release? See http://search.cpan.org/~timb/DBI/DBI.pm#CONTRIBUTING Tim.
Re: Convenience function selectall_hasharrayref
and here's the proposed patch hopefully my english is good enough. Tom Index: DBI.pm === --- DBI.pm (Revision 2381) +++ DBI.pm (Arbeitskopie) @@ -4014,7 +4014,17 @@ In which case the array is copied and each value decremented before passing to C/fetchall_arrayref. +The following example demonstrates the power of this method by fetching +all rows as an array-ref and addressing the row's column as hash-keys: + my $emps = $dbh-selectall_arrayref(select * from emp order by ename, { Slice = {} } ); + foreach my $emp ( @{ $emps } ) + { +print Employee: . $emp-{ename} . \n; + } + +When using an empty HashRef as Slice-Value all columns will be return in the Hash. + =item Cselectall_hashref $hash_ref = $dbh-selectall_hashref($statement, $key_field);
Re: Convenience function selectall_hasharrayref
On 2006-01-11 10:34:21 +0100, Peter J. Holzer wrote: On 2006-01-10 17:17:21 +, Tim Bunce wrote: Anything wrong with the existing my $emp = $dbh-selectall_arrayref(..., { Slice = {} }); ? Nothing, except that I didn't know about it. Looks like it was too obvious for me what fetchall_arrayref does (it returns an arrayref, duh!) so I never read what it really does (sometimes it returns a hashref). No, it doesn't. It returns an arrayref containing hashrefs. Obviously I can't distinguish between fetchall_arrayref and fetchrow_arrayref (at least not before finishing the first cup of coffee of the day). hp -- _ | Peter J. Holzer| If I wanted to be academically correct, |_|_) | Sysadmin WSR | I'd be programming in Java. | | | [EMAIL PROTECTED] | I don't, and I'm not. __/ | http://www.hjp.at/ | -- Jesse Erlbaum on dbi-users pgpZ4obFffhL7.pgp Description: PGP signature
Re: Convenience function selectall_hasharrayref
On 2006-01-10 17:17:21 +, Tim Bunce wrote: On Tue, Jan 10, 2006 at 12:47:54PM +0100, Peter J. Holzer wrote: Anyway, I rather frequently find that the most natural way to represent a query result is an array of hashes: Each row is hashref, but the the rows are in an array(ref) so that the order is preserved, and the columns can be accessed by name. [...] What do you think? Anything wrong with the existing my $emp = $dbh-selectall_arrayref(..., { Slice = {} }); ? Nothing, except that I didn't know about it. Looks like it was too obvious for me what fetchall_arrayref does (it returns an arrayref, duh!) so I never read what it really does (sometimes it returns a hashref). Please spread the word. Way too many people seem to not know about this! Now there are a few people less who don't know about it. Maybe an example in the man-page would make it clearer. OTOH, the DBI manpage is already very long for a manpage (pod2latex DBI.pm produces a 98 page document). hp -- _ | Peter J. Holzer| If I wanted to be academically correct, |_|_) | Sysadmin WSR | I'd be programming in Java. | | | [EMAIL PROTECTED] | I don't, and I'm not. __/ | http://www.hjp.at/ | -- Jesse Erlbaum on dbi-users pgpoQ1oYpWm1d.pgp Description: PGP signature
Re: Convenience function selectall_hasharrayref
Great. Thanks Tom. Tim. On Wed, Jan 11, 2006 at 01:10:43PM +0100, Tom Schindl wrote: and here's the proposed patch hopefully my english is good enough. Tom Index: DBI.pm === --- DBI.pm(Revision 2381) +++ DBI.pm(Arbeitskopie) @@ -4014,7 +4014,17 @@ In which case the array is copied and each value decremented before passing to C/fetchall_arrayref. +The following example demonstrates the power of this method by fetching +all rows as an array-ref and addressing the row's column as hash-keys: + my $emps = $dbh-selectall_arrayref(select * from emp order by ename, { Slice = {} } ); + foreach my $emp ( @{ $emps } ) + { +print Employee: . $emp-{ename} . \n; + } + +When using an empty HashRef as Slice-Value all columns will be return in the Hash. + =item Cselectall_hashref $hash_ref = $dbh-selectall_hashref($statement, $key_field);
Convenience function selectall_hasharrayref
or selectall_arrayhashref? Anyway, I rather frequently find that the most natural way to represent a query result is an array of hashes: Each row is hashref, but the the rows are in an array(ref) so that the order is preserved, and the columns can be accessed by name. Proposed use: my $emp = $dbh-selectall_hasharrayref(select * from emp order by ename); for (@$emp) { print $_-{ename} ($_-{empno}): $_-{job}\n; } or for ($first .. $last) { print $emp-[$_]{ename}, \n; } or something like that. What do you think? hp -- _ | Peter J. Holzer| If I wanted to be academically correct, |_|_) | Sysadmin WSR | I'd be programming in Java. | | | [EMAIL PROTECTED] | I don't, and I'm not. __/ | http://www.hjp.at/ | -- Jesse Erlbaum on dbi-users pgpuJM7iSMH8i.pgp Description: PGP signature
Re: Convenience function selectall_hasharrayref
Peter J. Holzer wrote or selectall_arrayhashref? Anyway, I rather frequently find that the most natural way to represent a query result is an array of hashes: Each row is hashref, but the the rows are in an array(ref) so that the order is preserved, and the columns can be accessed by name. Proposed use: my $emp = $dbh-selectall_hasharrayref(select * from emp order by ename); for (@$emp) { print $_-{ename} ($_-{empno}): $_-{job}\n; } or for ($first .. $last) { print $emp-[$_]{ename}, \n; } or something like that. What do you think? Would be sweet!! I have had the same idea myself as there are times when I could really use such a function. Would you like to work on it I have a few hours a day to commit to it?
Re: Convenience function selectall_hasharrayref
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Anyway, I rather frequently find that the most natural way to represent a query result is an array of hashes: Each row is hashref, but the the rows are in an array(ref) so that the order is preserved, and the columns can be accessed by name. It's a little hidden, but we already have this: $sth = $dbh-prepare($SQL); $sth-execute(@vals); $rows = $sth-fetchall_arrayref({}); for (@$rows) { print I am row $_-{id} with a value of $_-{fnord}\n; } - -- Greg Sabino Mullane [EMAIL PROTECTED] [EMAIL PROTECTED] PGP Key: 0x14964AC8 200601100745 http://biglumber.com/x/web?pk=2529DF6AB8F79407E94445B4BC9B906714964AC8 -BEGIN PGP SIGNATURE- iD8DBQFDw6zbvJuQZxSWSsgRAnu5AJ9Y6oKiBtS4CzHyY1pQDKtedoG4sQCfcGcD 9jZF9frISXkjo4xqGps+DaE= =l+IS -END PGP SIGNATURE-
Re: Convenience function selectall_hasharrayref
Peter J. Holzer wrote: or selectall_arrayhashref? Anyway, I rather frequently find that the most natural way to represent a query result is an array of hashes: Each row is hashref, but the the rows are in an array(ref) so that the order is preserved, and the columns can be accessed by name. Proposed use: my $emp = $dbh-selectall_hasharrayref(select * from emp order by ename); for (@$emp) { print $_-{ename} ($_-{empno}): $_-{job}\n; } or for ($first .. $last) { print $emp-[$_]{ename}, \n; } or something like that. What do you think? The names are to vague and not accurate selectall_arrayref_of_hashrefs() is what it is and anyone knows that immediately by looking at its name instead of having to look up its documentation
Re: Convenience function selectall_hasharrayref
On Tue, Jan 10, 2006 at 12:47:54PM +0100, Peter J. Holzer wrote: or selectall_arrayhashref? Anyway, I rather frequently find that the most natural way to represent a query result is an array of hashes: Each row is hashref, but the the rows are in an array(ref) so that the order is preserved, and the columns can be accessed by name. Proposed use: my $emp = $dbh-selectall_hasharrayref(select * from emp order by ename); for (@$emp) { print $_-{ename} ($_-{empno}): $_-{job}\n; } or for ($first .. $last) { print $emp-[$_]{ename}, \n; } or something like that. What do you think? Anything wrong with the existing my $emp = $dbh-selectall_arrayref(..., { Slice = {} }); ? Note that selectall_*arrayref* defines the outermost data that the method returns. What's inside that is controlled by attributes. For example, want an array or arrays where the inner arrays contain only the 2nd and 3rd fields? Easy: my $emp = $dbh-selectall_arrayref(..., { Slice = [2,3] }); Please spread the word. Way too many people seem to not know about this! Tim.