Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Two Dimensional Array Problem
I am trying to send the output of a mysql query to a two dimensional array.

This is what I've tried using push.

while (@results = $sth->fetchrow_array ())
{
$x = $results[0];
$y = $results[1];
push (@data,[$x],[$y]);
}

However, I don't get back a two dimensional array, I get back a single
array of @data.

Thanks. -Aaron

Report this thread to moderator Post Follow-up to this message
Old Post
ahuber@sdf.lonestar.org
06-04-05 01:55 AM


RE: Two Dimensional Array Problem
Looks like you're pushing a list on to another list. In effect, appending
one to the other. @data would be the first list. The second list would be
'$x, $y'. Variables in a comma delimited fashion is the same as a list or an
array.

I usually deal with multidimensional arrays this way:

$i = 0;
while (@results = $sth->fetchrow_array ())
{
$x = $results[0];
$y = $results[1];
@points = ($x, $y);
$data[$i] = \@points;
$i++;
}

I think that will work... maybe :)

-----Original Message-----
From: ahuber@sdf.lonestar.org [mailto:ahuber@sdf.lonestar.org]
Sent: Friday, June 03, 2005 2:42 PM
To: beginners@perl.org
Subject: Two Dimensional Array Problem


I am trying to send the output of a mysql query to a two dimensional array.

This is what I've tried using push.

while (@results = $sth->fetchrow_array ())
{
$x = $results[0];
$y = $results[1];
push (@data,[$x],[$y]);
}

However, I don't get back a two dimensional array, I get back a single
array of @data.

Thanks. -Aaron

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Report this thread to moderator Post Follow-up to this message
Old Post
Brian Barto
06-04-05 01:55 AM


RE: Two Dimensional Array Problem
ahuber@sdf.lonestar.org wrote:
> I am trying to send the output of a mysql query to a two dimensional
> array.=20
>=20
> This is what I've tried using push.
>=20
> while (@results =3D $sth->fetchrow_array ())
> {
>     $x =3D $results[0];
>     $y =3D $results[1];
>     push (@data,[$x],[$y]);

push( @data, [ $x , $y ] );
Access would be for first 0,0, 0,1, 1,0,1,1, etc

Wags ;)
> }
>=20
> However, I don't get back a two dimensional array, I get back a single
> array of @data.
>=20
> Thanks. -Aaron



 ****************************************
***************
This message contains information that is confidential
and proprietary to FedEx Freight or its affiliates.
It is intended only for the recipient named and for
the express purpose(s) described therein.
Any other use is prohibited.
 ****************************************
***************


Report this thread to moderator Post Follow-up to this message
Old Post
Wagner, David --- Senior Programmer Analyst --- WG
06-04-05 01:55 AM


Re: Two Dimensional Array Problem
ahuber@sdf.lonestar.org wrote:
> I am trying to send the output of a mysql query to a two dimensional array
.
>
> This is what I've tried using push.
>
> while (@results = $sth->fetchrow_array ())
> {
>     $x = $results[0];
>     $y = $results[1];
>     push (@data,[$x],[$y]);
> }
>
> However, I don't get back a two dimensional array, I get back a single
> array of @data.
>
> Thanks. -Aaron
>

Not sure if this is an exercise in how to build an AoA or if you are
really just trying to accomplish the goal and move on. In the latter
case, you can have DBI do it for you using 'fetchall_arrayref',

my $array_of_arrays = $sth->fetchall_arrayref;

use Data::Dumper;
print Dumper($array_of_arrays);

You should probably read the docs for the method, assuming you care
about data integrity and error handling,

http://search.cpan.org/~timb/DBI-1....tchall_arrayref

HTH,

http://danconia.org

Report this thread to moderator Post Follow-up to this message
Old Post
Wiggins d'Anconia
06-04-05 01:55 AM


RE: Two Dimensional Array Problem
That's much better than my method. Didn't know you could push blocks of data
like that.

Cool :)

-----Original Message-----
From: Wagner, David --- Senior Programmer Analyst --- WGO
[mailto:David.Wagner@freight.fedex.com]
Sent: Friday, June 03, 2005 3:01 PM
To: ahuber@sdf.lonestar.org; beginners@perl.org
Subject: RE: Two Dimensional Array Problem


ahuber@sdf.lonestar.org wrote:
> I am trying to send the output of a mysql query to a two dimensional
> array.
>
> This is what I've tried using push.
>
> while (@results = $sth->fetchrow_array ())
> {
>     $x = $results[0];
>     $y = $results[1];
>     push (@data,[$x],[$y]);

push( @data, [ $x , $y ] );
Access would be for first 0,0, 0,1, 1,0,1,1, etc

Wags ;)
> }
>
> However, I don't get back a two dimensional array, I get back a single
> array of @data.
>
> Thanks. -Aaron



 ****************************************
***************
This message contains information that is confidential
and proprietary to FedEx Freight or its affiliates.
It is intended only for the recipient named and for
the express purpose(s) described therein.
Any other use is prohibited.
 ****************************************
***************


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Report this thread to moderator Post Follow-up to this message
Old Post
Brian Barto
06-04-05 01:55 AM


RE: Two Dimensional Array Problem
brian.barto@spectrum-health.org wrote:
> That's much better than my method. Didn't know you could push blocks
> of data like that.
>=20
> Cool :)

Easiest way to see is use Data::Dumper and then print Dumper ( \@array =
).
Then you can see the setup.  Especially nice when you tell the Dumper to =
sort in order and you are using say a hash which has array.

Wags ;)
>=20
> -----Original Message-----
> From: Wagner, David --- Senior Programmer Analyst --- WGO
> [mailto:David.Wagner@freight.fedex.com]
> Sent: Friday, June 03, 2005 3:01 PM
> To: ahuber@sdf.lonestar.org; beginners@perl.org
> Subject: RE: Two Dimensional Array Problem
>=20
>=20
> ahuber@sdf.lonestar.org wrote: 
>=20
> 	push( @data, [ $x , $y ] );
> 	Access would be for first 0,0, 0,1, 1,0,1,1, etc
>=20
> Wags ;) 
>=20
>=20
>=20
>  ****************************************
***************
> This message contains information that is confidential
> and proprietary to FedEx Freight or its affiliates.
> It is intended only for the recipient named and for
> the express purpose(s) described therein.
> Any other use is prohibited.
>  ****************************************
***************
>=20
>=20
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>


Report this thread to moderator Post Follow-up to this message
Old Post
David --- Senior Programmer Analyst --- WGO Wagner
06-04-05 01:55 AM


Re: Two Dimensional Array Problem
[snip]

Hi Brian,

> I usually deal with multidimensional arrays this way:
>
> $i = 0;
> while (@results = $sth->fetchrow_array ())
> {
>    $x = $results[0];
>    $y = $results[1];
>    @points = ($x, $y);
>    $data[$i] = \@points;
>    $i++;
> }

Just a note about a possible problem with the statement:
$data[$i] = \@points;

Everytime through this loop, the reference to @points, (\@points), is
assigned to @data. But this is the same address. The result is that all the
array refs assigned to @data are the same. So, the *last* assignment to
@points, (x,y), will be the values for every element of @data.

A correct way to say it in your example would be:
$data[$i] = [@points];

Another would be to declare my @points in the loop, so that you have a fresh
ref every time through the loop. Then you could say:
$data[$i] = \@points;

Chris



Report this thread to moderator Post Follow-up to this message
Old Post
Chris Charley
06-04-05 01:55 AM


Re: Two Dimensional Array Problem
Always group reply so others can help and be helped, and to avoid
getting accidentally ignored.

"Because it's up-side down.
Why is that?
It makes replies harder to read.
Why not?
Please don't top-post." - Sherm Pendley, Mac OS X list

Aaron Huber wrote:
>
> On 6/3/05, Wiggins d'Anconia <wiggins@danconia.org> wrote:
> 
>
>
>
> Dear Wiggins,
>
> Thank you for taking the time to reply to my post.  Actually, what I
> am trying to do here is build an array that can be used with GD::Graph
> to show the graphical representation of a MySQL query.  This module
> only takes in an AoA like:
>
> @data = ([1,2,3,4],[10,20,30,40]); # @arr[$x vals], [$y vals]
>

Currently this is an array (specifically an array of array references,
which we call an array of arrays), but actually the subroutine call
below is taking an array reference.

> and outputs a graph using this:
>
> my $image = $plot->plot(\@data);
>

The notation above, C<\@data>, means you are passing an array reference.
A '' before a sigil causes a return of a reference to the structure.

perldoc perlreftut
perldoc perlref

> When I use this code with the above mentioned array hard coded in it
> works like a charm, but none of the suggestions on the message board
> gave me this type of array that I needed.  Is the above @data var an
> AoA or is it something else?
>

It is an array of arrays. If the other postings did not work then you
need to show us more of the code, specifically your select statement. We
are working blind at this point, we were assuming you were returning the
data in the correct way. Did you try the 'fetchall_arrayref' call?
Assuming your select is correct, and you really are getting one row of
data to plot at a time then it should be all you need. If it does not
work, then you probably need to manipulate your select differently, but
we can't tell that.

Use Data::Dumper to explore what structure you have. Try first printing
the above hard coded structure so you know what to look for. Then try
multiple things until you get it the same.

perldoc Data::Dumper

-- Sample --
use Data::Dumper;
my @aoa = ([1,2,3,4],[10,20,30,40]);

print Dumper(\@aoa);

> Thanks,
> Aaron Huber
>

HTH, good luck,

http://danconia.org

Report this thread to moderator Post Follow-up to this message
Old Post
Wiggins d'Anconia
06-06-05 01:55 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PERL Beginners archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 06:48 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.