[ic] accessing array

Mike Heins mikeh@minivend.com
Fri, 6 Apr 2001 15:10:33 -0400


Quoting Mat Jones - booksellersolutions.com (mat@booksellersolutions.com):
> Ok I think I should expand more on this....
> i dont think i provided enough info...here are snippets of my code
> on a particular page I do a sql search in perl :
> 
> my $resultset = $dbh->selectall_arrayref($query)
> 
> and want to use these results on another page so I do:
> 
> $Values->{final} = @$resultset;
> 
> This maybe where I am going wrong..
> on another page I try to access the stored value :
> 
> return $Values->{final};
> 
> returns ARRAY(0x8d9154c)ARRAY(0x8dd4314)ARRAY(0x8d5a8bc)ARRAY(0x8d81490) to
> the screen and:
> 
> return $Values->{final}[0];
> 
> returns nothing.... I am thinking this is because the initial value was an
> array ref and somehow is not being dereferenced...

This is mostly a Perl question, but I don't answer many of those these
days. 8-)

selectall_arrayref returns exactly what it says, all in arrayref form.

	select sku, description, price from products limit 10

	would yield 
	[
		['gift_cert', 'Gift Certificate', '1.00'],
		['os28004', 'Ergo Roller', '21.99'],
		['os28005', 'Trim Brush', '8.99'],
		['os28006', 'Painters Brush Set', '29.99'],
		['os28007', 'Disposable Brush Set', '14.99'],
		['os28008', 'Painters Ladder', '29.99'],
		['os28009', 'Brush Set', '9.99'],
		['os28011', 'Spackling Knife', '14.99'],
		['os28044', 'Framing Hammer', '19.99'],
		['os28057a', '16 Penny Nails', '14.99'],
	]

If you want just the keys joined together as text (i.e. element 0), do:

	$Values->{text_final} = join " ", map { $_->[0] } @$resultset;

If you want the array of only the code results, do:

	$Values->{arrayref_final} = [ map { $_->[0] } @$resultset ]

-- 
Red Hat, Inc., 131 Willow Lane, Floor 2, Oxford, OH  45056
phone +1.513.523.7621 fax 7501 <mheins@redhat.com>

I have a cop friend who thinks he ought be able to give a new ticket;
"too dumb for conditions".