Re: Dereferencing fetchall_arrayref({})
by dragonchild (Archbishop) on Apr 25, 2004 at 20:34 UTC
|
Y'all are completely wrong. Read the DBI POD before spouting. fetchall_arrayref() does return an array of arrays, but fetchall_arrayref({}) returns an array of hashes.
The reason why the first code works is because the TMPL_LOOP selscene is expecting an array of hashes. When you work with it, $data->[1] is a hashref. So, what you want to do is:
Example A, rewritten:
my $data = $sth->fetchall_arrayref({});
if ($data->[0]{title} eq 'large') {
ucase( $data->[0]{title} );
}
print "First title: '$data->[0]{title}'\n";
$template->param( selscene => $data );
A few comments, in no particular order:
------
We are the carpenters and bricklayers of the Information Age.
Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose
| [reply] [d/l] [select] |
|
|
| [reply] |
Re: Dereferencing fetchall_arrayref({})
by Zaxo (Archbishop) on Apr 25, 2004 at 20:03 UTC
|
DBI::fetchall_arrayref returns an array of arrays. You need two indexes to dereference a data element. Let your code read,
if ( $data->[0][1] eq "large" ) { #something like this or...
$data->[0][1] = uc ( $data->[0][1] );
}
Note that ucase is spelled uc, and does not modify its argument in-place.
| [reply] [d/l] |
|
|
| [reply] [d/l] [select] |
|
|
| [reply] [d/l] |
|
|
my $data;
while ($_ = $sth->fetchall_arrayref()) {
uc(${$_}{'title'}) if(${$_}{'title'} eq "large");
push @{$data}, $_;
}
--
b10m
All code is usually tested, but rarely trusted.
| [reply] [d/l] |
|
|
|
|
Re: Dereferencing fetchall_arrayref({})
by blue_cowdawg (Monsignor) on Apr 25, 2004 at 20:03 UTC
|
but would like to know how to access the data (see example A),
This is one of those moments when as an adjunct professor
I always take time out to tell my students how the watch
works before I tell them what time it is.
First off a quick review of what an array reference is:
# make an array reference:
my $ref = \@rry;
#
# or an anoonymous array:
my $ref2=[ "stuff", "things", "goodies"];
</ocde>
If I have a function returning an arrey reference:
<code>
my $rows=$sth->fetchall_arrayref;
Then to dereference it I would do the following:
my @array=@{$sth->fetchall_arrayref};
But wait! If I am passing an array to
HTML::Template's param method then I'm going
to want to pass an anoymous array such as:
$template->param(selscene => [ @array ] );
Hope my brain fried ramblings make sense and help...
| [reply] [d/l] [select] |
When data structures differ from your expectations
by TomDLux (Vicar) on Apr 26, 2004 at 00:15 UTC
|
Other people have provided the information you needed in this case, but you don't want to go seeking out help every time a reference doesn't work the way you imagine it should.
When $data->[1] did not return the type of values you anticipated, the next step is to find out what it does contain .... if your visualization of relaity and the contents of the data structure disagree, only one of them can be right. The appropriate solution at that point is to use Data::Dumper:
use Data::Dumper;
print Dumper $data;
--
TTTATCGGTCGTTATATAGATGTTTGCA
| [reply] [d/l] |
|
|
| [reply] |
|
|
When I got into Perl, although I had a degree and many years of programming experience, it took me a long time to get used to complex data structures and references. I was used to C, where you have the structure declaration to refer to; since Perl can generate complex structures without any indication of what the arrangement will be, it can be hard to figure out.
For the first six months, I mostly used data structures and references when I had to; the next six months I had to code-and-debug, a bit at a time, to figure out the appropriate code. By the end of the year, it had become part of me, I prefered passing a reference instead of a list, and then naturally stored that reference in an array or hash, and then stored a reference to that in ....
Like all good things, data structures and references look like a long struggle when you're looking up at the learning curve, but it's definitely worth the effort.
TomDLux
--
TTTATCGGTCGTTATATAGATGTTTGCA
| [reply] |
Re: Dereferencing fetchall_arrayref({})
by EdwardG (Vicar) on Apr 25, 2004 at 20:07 UTC
|
Fetchall arrayref returns a reference to an Array of Arrays, so to reference an item of data you need an extra subscript, like this
$data->[0]->[0]
You can see this clearly by dumping the reference, either in the debugger or with Data::Dumper.
It might help to conceptualise the referenced data as a set of rows, each row having a set of columns.
Update: I'm wrong about the AoA, (nicely picked up by dragonchild) but advice about debugger (use 'x') and Data::Dumper still stand. | [reply] [d/l] |