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 [EMAIL PROTECTED] wrote: [EMAIL PROTECTED] 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.48/DBI.pm#fetchall_arrayref HTH, http://danconia.org 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([EMAIL PROTECTED]); The notation above, C[EMAIL PROTECTED], 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([EMAIL PROTECTED]); Thanks, Aaron Huber HTH, good luck, http://danconia.org -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
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: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
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] = [EMAIL PROTECTED]; $i++; } I think that will work... maybe :) -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] 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: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: Two Dimensional Array Problem
[EMAIL PROTECTED] 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: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Two Dimensional Array Problem
[EMAIL PROTECTED] 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.48/DBI.pm#fetchall_arrayref HTH, http://danconia.org -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
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:[EMAIL PROTECTED] Sent: Friday, June 03, 2005 3:01 PM To: [EMAIL PROTECTED]; beginners@perl.org Subject: RE: Two Dimensional Array Problem [EMAIL PROTECTED] 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: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: Two Dimensional Array Problem
[EMAIL PROTECTED] wrote: That's much better than my method. Didn't know you could push blocks of data like that. Cool :) Easiest way to see is use Data::Dumper and then print Dumper ( [EMAIL PROTECTED] ). 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 ;) -Original Message- From: Wagner, David --- Senior Programmer Analyst --- WGO [mailto:[EMAIL PROTECTED] Sent: Friday, June 03, 2005 3:01 PM To: [EMAIL PROTECTED]; beginners@perl.org Subject: RE: Two Dimensional Array Problem [EMAIL PROTECTED] 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: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
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] = [EMAIL PROTECTED]; $i++; } Just a note about a possible problem with the statement: $data[$i] = [EMAIL PROTECTED]; Everytime through this loop, the reference to @points, ([EMAIL PROTECTED]), 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] = [EMAIL PROTECTED]; 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] = [EMAIL PROTECTED]; Chris -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response