# How initialize an array to 0's?

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on

I have the following correct sentence

that doesn't do anything useful.

What is the proper and modern mode to do the thing?

Thanks

## Re: How initialize an array to 0's?

Do you really mean to have a 2d array?
Anyway, use slices. Plus maybe some perl-ish tricks to quickly generate
the list of 0s.
In the 1d case:
#be careful, you probably want to start indexing at 0, not 1
#leaving it your way for now
If you really meant 2d then:
@flagi;
\$flagi[\$jornadas]=undef;#the first dimension is grown to have
#again, you probably want indices from 0 to \$jornados-1.
#but maybe you don't for some reason? I'll leave your way here too.
map @flagi;
foreach my \$row (@flagi){
@=split(/\s/,"0 "x\$partidos);
}
This code is untested but should be more  or less correct.
One more time, be careful with your indices! perl starts indexing at 0,
not 1.

On 6/14/2011 12:02 PM, gamo@telecable.es wrote:

## Re: How initialize an array to 0's?

Whoops, in that 1d case this line

So, the number of elements is correct

## Re: How initialize an array to 0's?

RM> Whoops, in that 1d case this line

RM> So, the number of elements is correct

Are you aware of what (0) x 20 does?  split() is completely unnecessary.

Ted

## Re: How initialize an array to 0's?

No, actually, I didn't!
Has this always been in the language or is it a new-ish feature?
I didn't know the x operator worked with lists.
Cool stuff. Thanks.

On 6/14/2011 12:06 PM, Ted Zlatanov wrote:

## Re: How initialize an array to 0's?

RM> On 6/14/2011 12:06 PM, Ted Zlatanov wrote:

RM> No, actually, I didn't!
RM> Has this always been in the language or is it a new-ish feature?

At least since 5.005, probably earlier too.

Ted

sherm--

## Re: How initialize an array to 0's?

wrote:

To be fair, he mostly mocks the posting guidelines ;)

Ted

## Re: How initialize an array to 0's?

True! I did miss the mark a bit. Just too tempting to resist though... :-)

sherm--

## Re: How initialize an array to 0's?

Feel free to point out where this is covered in the faq.
http://faq.perl.org /

On 6/14/2011 12:41 PM, Sherm Pendley wrote:

## Re: How initialize an array to 0's?

RM> No, actually, I didn't!
RM> Has this always been in the language or is it a new-ish feature?
RM> I didn't know the x operator worked with lists.
RM> Cool stuff. Thanks.

and yet you flame and troll about everyone else here and the
guidelines. just more proof of your uselessness here. go away. you make
moronzilla look good.

uri

--
Uri Guttman  ------  uri@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------

## Re: How initialize an array to 0's?

What is a "moronzilla" ?
I think you are not a very nice person.
Perhaps it is because you weigh 400 pounds and reek of faeces.
Anyway, my way seems to work although perhaps it isn't the most efficient.
At least I offered a working solution instead of whinging and making
weird references.

On 6/14/2011 12:50 PM, Uri Guttman wrote:

## Re: How initialize an array to 0's?

RM> What is a "moronzilla" ?

See http://www.perlmonks.org/?node_id=404291 but really, if you didn't
live through it, it's hard to explain.

Ted

## Re: How initialize an array to 0's?

Ah! I see! I did Google Groups search for purl gurl and got all sorts
of bizarre stuff.
Thanks for the tip.

On 6/14/2011 1:25 PM, Ted Zlatanov wrote:

## Re: How initialize an array to 0's?

On 14.06.2011 17:58, Ralph Malph wrote:

What is this?

First you create a string, then you split it again? Further, you assign
the value "0", not 0.

better use:
@array[1..\$n] = (0) x \$n;

For 2d arrays (= an array of array references), you will need to loop
over the first array and run the above command for every array element.

Wolf

## Re: How initialize an array to 0's?

Wolf Behrenhoff wrote:
) On 14.06.2011 17:58, Ralph Malph wrote:
)> Whoops, in that 1d case this line
)> So, the number of elements is correct
)
) What is this?
)
) First you create a string, then you split it again? Further, you assign
) the value "0", not 0.
)
) better use:
)   @array[1..\$n] = (0) x \$n;
)
) For 2d arrays (= an array of array references), you will need to loop
) over the first array and run the above command for every array element.

Doesn't:
@array = ([(0) x \$n]) x \$n;
work ?

(Yes, I know it doesn't work, the interesting bit is in why it doesn't.)

SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT

## Re: How initialize an array to 0's?

W> Doesn't:
W>   @array = ([(0) x \$n]) x \$n;
W> work ?

W> (Yes, I know it doesn't work, the interesting bit is in why it doesn't.)

It copies the array reference 20 times, which is probably not wanted.

Here's what I would use for a 20x20:

@array = map {[(0) x 20]} (1..20);

Or, for triangular fun:

@array = map {[(0) x \$_]} (1..20);

Ted

## Re: How initialize an array to 0's?

'THIS' x 5,  (THAT) x 5,  ([REF]) x 5
It appears only a single thing is constructed and duplicated
only in assingment or when referenced.
This might be a design consideration to save memory.

So, for example
@array = ([(0) x 5]) x 3;
does work, but @array elements all have the same reference to [0,0,0,0,0]

Compare that with
@array = eval "[(0) x 5]," x 3;
where each element has a different reference.

-sln

## Re: How initialize an array to 0's?

------------------
#!/usr/bin/perl

use strict;
use warnings;

my \$partidos = 7;

my \$flagi = [ undef, ( [ undef, ( 0 ) x \$partidos ] ) x \$jornados ];

for my \$j ( 1..\$jornados ) {
for my \$p ( 1..\$partidos ) {
print ' ', \$flagi->[\$j][\$p];
}
print "\n";
}
------------------

This gives the original poster his desired origin 1 indexing.

For origin 0 indexing:
my \$flagi = [ ( [ ( 0 ) x \$partidos ] ) x \$jornados ];

--
David Canzi        | Life is too short to point out every mistake. |

## Re: How initialize an array to 0's?

DC> For origin 0 indexing:
DC> my \$flagi = [ ( [ ( 0 ) x \$partidos ] ) x \$jornados ];

as mentioned elsewhere, that assigns the same anon array reference to
each of the lower level entries. if you modify any value in that array,
it will show up in all the copies. you need to use map to create new
array refs for each entry.

uri

--
Uri Guttman  ------  uri@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------