Default initial array value? [TMTOWTDI]

H

h3xx

I know this kind of goes against traditional programming practices,
but here goes:

I need to initialize an array to all zeroes to begin with, but the
only way I see is to set each index manually:

my $ary = [ map {0} 0 .. $last_index ];

Because the indices invariably get set to undef if I just set the last
index. I've read the perlvar manpage over and over again and can't
find a relevant variable to determine what undefined array indices get
set to instead of undef.

My question is, Is there more than one way to do this?
 
H

h3xx

I know this kind of goes against traditional programming practices,
but here goes:

I need to initialize an array to all zeroes to begin with, but the
only way I see is to set each index manually:

my $ary = [ map {0} 0 .. $last_index ];

Because the indices invariably get set to undef if I just set the last
index. I've read the perlvar manpage over and over again and can't
find a relevant variable to determine what undefined array indices get
set to instead of undef.

My question is, Is there more than one way to do this?

I mean, I can think of this:

$ary = [ split //, '0' x $num_indices ];

But that's dealing with strings parsing and regex matching, which is
slower than what I'm thinking of.
 
J

John W. Krahn

h3xx said:
I know this kind of goes against traditional programming practices,
but here goes:

I need to initialize an array to all zeroes to begin with, but the
only way I see is to set each index manually:

my $ary = [ map {0} 0 .. $last_index ];

Because the indices invariably get set to undef if I just set the last
index. I've read the perlvar manpage over and over again and can't
find a relevant variable to determine what undefined array indices get
set to instead of undef.

My question is, Is there more than one way to do this?

my @ary = ( 0 ) x $size;



John
 
H

h3xx

h3xx said:
I know this kind of goes against traditional programming practices,
but here goes:
I need to initialize an array to all zeroes to begin with, but the
only way I see is to set each index manually:
my $ary = [ map {0} 0 .. $last_index ];
Because the indices invariably get set to undef if I just set the last
index. I've read the perlvar manpage over and over again and can't
find a relevant variable to determine what undefined array indices get
set to instead of undef.
My question is, Is there more than one way to do this?

my @ary = ( 0 ) x $size;

John

Nice! Thanks. I had no idea about that trick!
 
S

sln

I know this kind of goes against traditional programming practices,
but here goes:

I need to initialize an array to all zeroes to begin with,
[snip]

There is never a need to do this.
-sln
 

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
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top