$# and 2d arrays

D

Dieter D'Hoker

quick questions,

if i have an aray @array that i'm going to fill :

for (my $y=0; $y<20; $y++) {
for (my $x=0; $x<20; $x++) {
$field[$x][$y] = 0;
}
}

should i use $#array = 19;
or $#array = 399; ?
to predefine the correct lenght ?
 
S

Sherm Pendley

Dieter said:
if i have an aray @array that i'm going to fill :

should i use $#array = 19;
or $#array = 399; ?
to predefine the correct lenght ?

What leads you to think you need to predefine the length of an array at all?
This is Perl, not C. Just assign the values you want to the array elements
- the array will take care of itself, resizing as needed.

sherm--
 
E

Eric Schwartz

Dieter D'Hoker said:
if i have an aray @array that i'm going to fill :

for (my $y=0; $y<20; $y++) {
for (my $x=0; $x<20; $x++) {
$field[$x][$y] = 0;
}
}

Two problems:

1) You're referring to @field here, not @array. Which is it?

2) C-style loops are ugly. Assuming you are going to do
something like this, try a more Perlish approach:

foreach my $x (0..19) {
foreach my $y (0..19) {
$field[$x][$y] = 0;
}
}

should i use $#array = 19;
or $#array = 399; ?
to predefine the correct lenght ?

The answer is neither, as a rule. Why do you feel you need to
predefine the length of the array at all? I suppose if you had
several thousand entries it might make sense, but for the example you
posted, it's a waste of time. So far, I have never written a program
in Perl that would benefit significantly from preallocating arrays,
and I've been coding in Perl for about 7 years now.

That said, if you absolutely feel you must, and I emphasize again that
you probably do not need to do this, the answer lies in what a '2d
array' really is in Perl: an array of arrayrefs. So the top-level
array only should be predefined to have as many entries in it as $x
will index.

-=Eric
 
B

Bob Walton

Dieter D'Hoker wrote:

....

if i have an aray @array that i'm going to fill :

field?---------------^^^^^
You used the variable @field in your example below??

for (my $y=0; $y<20; $y++) {
for (my $x=0; $x<20; $x++) {
$field[$x][$y] = 0;
}
}

should i use $#array = 19;
field?-----------^^^^^


or $#array = 399; ?
field?-^^^^^


to predefine the correct lenght ?

Well, Perl's "2D" arrays are really a set of 1D arrays. The "outer"
array contains references to a set of "inner" anonymous (i.e., nameless)
arrays. So you should use $#field=19; in your example above, if that is
what you want to do (there probably actually is *very* little point to
predefining the array). But if you are going to do that, you should
also predefine the inner arrays as well. But the code you have will
store the 0th element of each of the 20 inner arrays, then the 1st
element, etc. So your outer for loop should be on $x rather than $y if
you want to do that without adding another loop. Something like:

my @field;
$#field=19; #predefine length of outer array @field
for my $x (0..19){
$#{$field[$x]}=19; #predefine length of anonymous array
for my $y (0..19){
$field[$x][$y]=0;
}
}

But again, there is probably not much point in predefining the array length.
 
D

Dieter D'Hoker

Bob said:
my @field;
$#field=19; #predefine length of outer array @field
for my $x (0..19){
$#{$field[$x]}=19; #predefine length of anonymous array
for my $y (0..19){
$field[$x][$y]=0;
}
}

But again, there is probably not much point in predefining the array
length.

okay thx for the response,
sorry for the mixup between @field and @array, i was typing the message when
i saw i got the code open on my other screen and just copy paste it :)

well, i have made an AI that has to make 1000's of those fields and evaluate
them within a least every second,
but the more it can do the better ... So i thought predifining the arrays
might give it a little edge ?
 
D

Dieter D'Hoker

Eric said:
Dieter D'Hoker said:
if i have an aray @array that i'm going to fill :

for (my $y=0; $y<20; $y++) {
for (my $x=0; $x<20; $x++) {
$field[$x][$y] = 0;
}
}

Two problems:

1) You're referring to @field here, not @array. Which is it?

i made a mistake @field = @array , sorry for the mixup
2) C-style loops are ugly. Assuming you are going to do
something like this, try a more Perlish approach:

foreach my $x (0..19) {
foreach my $y (0..19) {
$field[$x][$y] = 0;
}
}


besides being less ugly, is this also faster ?


The answer is neither, as a rule. Why do you feel you need to
predefine the length of the array at all? I suppose if you had
several thousand entries it might make sense, but for the example you
posted, it's a waste of time. So far, I have never written a program
in Perl that would benefit significantly from preallocating arrays,
and I've been coding in Perl for about 7 years now.

well i have an AI that tries to find the best move on a 20*20 field,
therefore it tries all possible moves and evaluates the resulting field,
this results in about 48 fields that are created,
but to make it smarter it then tries to lookahead every 48 fields result in
another 48 fields ,
resulting in 2304 fields, etc. And all this has to be done in realtime, the
AI has to give a response within very little time.

So i'm trying to optimize the code wherever possible, ..
 
A

Ala Qumsieh

Dieter said:
quick questions,

if i have an aray @array that i'm going to fill :

for (my $y=0; $y<20; $y++) {
for (my $x=0; $x<20; $x++) {
$field[$x][$y] = 0;
}
}

should i use $#array = 19;
or $#array = 399; ?
to predefine the correct lenght ?

Neither. Generally, you don't need to, but if you had to initialize
everything to 0 (if undef is not good enough) then I would suspect that
the following would be faster:

my @field;
$field[$_] = [(0) x 20] for 0 .. 19;

I didn't benchmark though.

--Ala
 
B

Bob Walton

Dieter said:
Bob Walton wrote:

my @field;
$#field=19; #predefine length of outer array @field
for my $x (0..19){
$#{$field[$x]}=19; #predefine length of anonymous array
for my $y (0..19){
$field[$x][$y]=0;
}
}

But again, there is probably not much point in predefining the array
length.

okay thx for the response,
sorry for the mixup between @field and @array, i was typing the message when
i saw i got the code open on my other screen and just copy paste it :)


Copy/paste is *definitely* the way to do it. Code is bad enough without
typos.

well, i have made an AI that has to make 1000's of those fields and evaluate
them within a least every second,
but the more it can do the better ... So i thought predifining the arrays
might give it a little edge ?


Yeah, maybe. You could

use Benchmark;

to find out. But if you're that concerned about speed, you probably
want to use C or Fortran instead of Perl.
 
J

John W. Krahn

Ala said:
if i have an aray @array that i'm going to fill :

for (my $y=0; $y<20; $y++) {
for (my $x=0; $x<20; $x++) {
$field[$x][$y] = 0;
}
}

should i use $#array = 19;
or $#array = 399; ?
to predefine the correct lenght ?

Neither. Generally, you don't need to, but if you had to initialize
everything to 0 (if undef is not good enough) then I would suspect that
the following would be faster:

my @field;
$field[$_] = [(0) x 20] for 0 .. 19;

Or:

my @field = map [ (0) x 20 ], 0 .. 19;


John
 
D

Dieter D'Hoker

John said:
Ala said:
if i have an aray @array that i'm going to fill :

for (my $y=0; $y<20; $y++) {
for (my $x=0; $x<20; $x++) {
$field[$x][$y] = 0;
}
}

should i use $#array = 19;
or $#array = 399; ?
to predefine the correct lenght ?

Neither. Generally, you don't need to, but if you had to initialize
everything to 0 (if undef is not good enough) then I would suspect
that the following would be faster:

my @field;
$field[$_] = [(0) x 20] for 0 .. 19;

Or:

my @field = map [ (0) x 20 ], 0 .. 19;

okay, thx for the suggestions ...
 
J

Joe Smith

Dieter said:
well i have an AI that tries to find the best move on a 20*20 field,

$array[$_] = [ (0) x 20 ] for (0 .. 19);
therefore it tries all possible moves and evaluates the resulting field,
this results in about 48 fields that are created,
but to make it smarter it then tries to lookahead every 48 fields result in
another 48 fields, resulting in 2304 fields, etc.

$fields[$_] = [ (0) x 48 ] for (0 .. 47);
And all this has to be done in realtime, the
AI has to give a response within very little time.
So i'm trying to optimize the code wherever possible, ..

Sounds like premature optimization. Profile the code first,
find out where the hot spots really are.
-Joe
 
B

Bob Walton

Dieter said:
....


well i have an AI that tries to find the best move on a 20*20 field,
therefore it tries all possible moves and evaluates the resulting field,


Just a general comment: something that does an exhaustive search
doesn't sound much like "AI". Your speed might be improved dramatically
by using learning algorithms and the like in place of exhaustive searches.


....
So i'm trying to optimize the code wherever possible, ..

You'll likely gain way *way* more from optimizing algorithms than
optimizing code.
 
A

Anno Siegel

John W. Krahn said:
Ala Qumsieh wrote:
[...]
my @field;
$field[$_] = [(0) x 20] for 0 .. 19;

Or:

my @field = map [ (0) x 20 ], 0 .. 19;

Nitpick: I'd write "1 .. 20" instead of "0 .. 19". It makes it easier
to see that the array is square.

Anno
 

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

No members online now.

Forum statistics

Threads
474,150
Messages
2,570,853
Members
47,394
Latest member
Olekdev

Latest Threads

Top