2D x dimension length

D

doormouse

Perhaps I'm going about this wrong for PERL. I'm working on something
that rolls over database results. I need to spend a long time on the
result set, so I wanted to pull the query data out, close the db
connection and parse/reconnect as necessary.

While the result set is coming out I'm storing the data in to a 2d
array. At the end of the store phase I am able to pull array length
from the Y dimension but not the X.

Here's a simulation that explains my situation
---begin code---
#this is actually a query in the real code, each return plugging in to
an individual iteration.
#the number of column and rows are both variable in scope.
$thisArray[0][0] = "test";
$thisArray[0][1] = "test";
$thisArray[0][2] = "test";
$thisArray[1][0] = "test";
$thisArray[1][1] = "test";
$thisArray[1][2] = "test";
$thisArray[2][0] = "test";
$thisArray[2][1] = "test";
$thisArray[2][2] = "test";


#Preserve the scalar
$oneSize = @thisArray;
print $oneSize;
print "\n";

#point to a single dimesion, and try to do the same
@twoArr = @thisArray[0];
$twoSize = @twoArr;
print $twoSize;
print "\n";

---end code---

$oneSize comes out correctly as 3, $twoSize always comes out as 1.

I also attempted:

---begin code---

$twosize = @thisArray[0];
print $twosize;
print "\n";

---end code---
but this just brings out ARRAY(#x#######)

I'm more of a lazy PHP wheenie, so my methods might be well off the
mark of what I should be doing. I just haven't been able to find
anyone with this exact problem after quite some googling (perhaps an
indication that I'm not heading in the right direction)

any assistance you could afford me would be greatly appreciated,
Thanks in advance,
--Mike
 
B

Bart Van der Donck

[...]
While the result set is coming out I'm storing the data in to a 2d
array. At the end of the store phase I am able to pull array length
from the Y dimension but not the X.

Here's a simulation that explains my situation
---begin code---
#this is actually a query in the real code, each return plugging in to
an individual iteration.
#the number of column and rows are both variable in scope.
$thisArray[0][0] = "test";
$thisArray[0][1] = "test";
$thisArray[0][2] = "test";
$thisArray[1][0] = "test";
$thisArray[1][1] = "test";
$thisArray[1][2] = "test";
$thisArray[2][0] = "test";
$thisArray[2][1] = "test";
$thisArray[2][2] = "test";

#Preserve the scalar
$oneSize = @thisArray;
print $oneSize;
print "\n";

#point to a single dimesion, and try to do the same
@twoArr = @thisArray[0];
$twoSize = @twoArr;
print $twoSize;
print "\n";

---end code---

$oneSize comes out correctly as 3, $twoSize always comes out as 1.
[...]

my $X_max = my $Y_max = -1;
for my $i ( 0 .. $#thisArray ) {
for my $j ( 0 .. $#{ $thisArray[$i] } ) {
if (defined $thisArray[$i][$j]) {
$X_max = $i if ($i > $X_max);
$Y_max = $j if ($j > $Y_max);
}
}
}
print "$X_max\n$Y_max\n";
 
B

Bart Van der Donck

Perhaps I'm going about this wrong for PERL. I'm working on something
that rolls over database results. I need to spend a long time on the
result set, so I wanted to pull the query data out, close the db
connection and parse/reconnect as necessary.

While the result set is coming out I'm storing the data in to a 2d
array. At the end of the store phase I am able to pull array length
from the Y dimension but not the X.

Here's a simulation that explains my situation
---begin code---
#this is actually a query in the real code, each return plugging in to
an individual iteration.
#the number of column and rows are both variable in scope.
$thisArray[0][0] = "test";
$thisArray[0][1] = "test";
$thisArray[0][2] = "test";
$thisArray[1][0] = "test";
$thisArray[1][1] = "test";
$thisArray[1][2] = "test";
$thisArray[2][0] = "test";
$thisArray[2][1] = "test";
$thisArray[2][2] = "test";
[...]

If you just want to "roll over the results" (the offical term would be
"walk through"), there should be no direct need to know your array
length.

#!/usr/bin/perl
use strict;
use warnings;
my @thisArray;
$thisArray[0][0] = "test1";
$thisArray[0][1] = "test2";
$thisArray[0][2] = "test3";
$thisArray[1][0] = "test4";
$thisArray[1][1] = "test5";
$thisArray[1][2] = "test6";
$thisArray[2][0] = "test7";
$thisArray[4][3] = "test8";
$thisArray[6][3] = "test9";
for my $i ( 0 .. $#thisArray ) {
for my $j ( 0 .. $#{ $thisArray[$i] } ) {
print "value on $i $j is "
.$thisArray[$i][$j]."\n"
if $thisArray[$i][$j]
}
}
 
P

Paul Lalli

(e-mail address removed) wrote:

#Preserve the scalar
$oneSize = @thisArray;
print $oneSize;
print "\n";

#point to a single dimesion, and try to do the same
@twoArr = @thisArray[0];

This is a problem Assuming @thisArray is in fact a 2d array (that is,
an aray of array references), then @thisArray[0] is an array slice - a
list containing the first element of the array, which is an array
reference. Therefore @twoArr is an array containing a single array
reference.
$twoSize = @twoArr;

Now $twoSize gets the size of @twoArr, which is one, because it
contains only one single array reference.
print $twoSize;
print "\n";

---end code---

$oneSize comes out correctly as 3, $twoSize always comes out as 1.

As expected.
I also attempted:

---begin code---

$twosize = @thisArray[0];

Once again, @thisArray[0] is an array slice, containing exactly one
element. In this case, the list used in scalar context returns the
last element (which is the *only* element here), so $twosize is a
single array reference.
print $twosize;
print "\n";

---end code---
but this just brings out ARRAY(#x#######)

As expected.
I'm more of a lazy PHP wheenie, so my methods might be well off the
mark of what I should be doing. I just haven't been able to find
anyone with this exact problem after quite some googling (perhaps an
indication that I'm not heading in the right direction)

You need to read the documentation. You have misperceptions both about
how to access a single element of an array (you use $ for all single
elements, @ for list slices), and have apparently never heard of
dereferencing.

perldoc perldata
perldoc perlreftut
perldoc perllol
any assistance you could afford me would be greatly appreciated,

Assuming @thisArray is a 2d array, then $thisArray[0] (the first
element of the array) is a reference to an array. To dereference a
reference, you enclose the whole thing in { }, and prepend the
appropriate sigil ( @, in this case).

@{$thisArray[0]} is the array that $thisArray[0] references. You can
then find the size of this array just like you find the size of any
other array - evaluate it in scalar context:

my $size = @{$thisArray[0]};

Hope this helps,
Paul Lalli
 
T

Tad McClellan

Perhaps I'm going about this wrong for PERL.


There is no "PERL".

There is "Perl" and there is "perl" though.

At the end of the store phase I am able to pull array length
from the Y dimension but not the X.


"Y" is the 1st level and "X" is the 2nd level?

Inverting conventions like that is likely to lead to confusion...

@twoArr = @thisArray[0];


You should always enable warnings when developing Perl code!

Have you seen the Posting Guidelines that are posted here frequently?

$twosize = @thisArray[0];
but this just brings out ARRAY(#x#######)


That means that you did not dereference the reference.

any assistance you could afford me would be greatly appreciated,


perldoc perlreftut


then apply "Use Rule 1", which I like to do in 3 steps:

$twosize = @plain; # pretend it is a plain array

$twosize = @{ }; # replace the array name with a block

$twosize = @{ $thisArray[0] }; # fill in the block with a reference
 
D

doormouse

Paul,

That is exaclty what I was looking for. It would seem I have a lot of
reading to do on the innerworkings of Perl's references. I've picked
up the O'Reilly nutshell book to help me along, It appears I'm missing
a bit on the concepts and terminology, but now I'm aware of what I'm
missing and can study up of the pieces that are different from what I'm
expecting.

I appreciate everyone's time and effort to push me along in the right
direction.
 
P

Paul Lalli

doormouse said:
That is exaclty what I was looking for.

Please quote context when you post a reply, as I am doing now.
Otherwise, not everyone has the ability to follow along with what
you're saying. (I am only able to do so because I *happen* to be using
a newsreader that threads, and the messages *happened* to arrive at my
newsserver in their correct order. Neither of these are guaranteed.)
It would seem I have a lot of
reading to do on the innerworkings of Perl's references. I've picked
up the O'Reilly nutshell book to help me along, It appears I'm missing
a bit on the concepts and terminology,

"Nutshell" books are good for quick refreshers, and are not suitable
(IMHO) for actually learning a language feature. I strongly recommend
"Intermediate Perl" by Randal Schwartz, Tom Phoenix, and brian d foy.
It is a direct sequel to the industry-standard "Learning Perl" (ie, the
Llama).

But see also those perldocs I mentioned:
perldoc perldata
perldoc perlreftut
perldoc perllol
as well as...
perldoc perldsc
perldoc perlref

Paul Lalli
 
D

doormouse

Tad,

You have my apologies on misquoting 'Perl' as an acronym, thereby
throwing more gasoline on the (unbenounced to me) "Perl is not an
acronym" flame war.

The Y vs X I used was columns vs. rows in a 2d structure. Probably not
the best reference I could have used because it loses all validity in a
2d model.

It would appear I need to read up on arrays in Perl, they are
definately more sophisticated than what i'm used to dealing with.

Thanks for you time and effort,
--Mike
 
D

doormouse

Bart,

Thanks for the code, Seeing where I'm going wrong in implimentation is
very useful, I'll use what you've provided to help me impliment this
script, then bone up on the material so I don't bug you guys with more
junior level language questions.

Thanks for your time and effort,
--Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top