views:

118

answers:

3

Hi,

How do I initialize an array to 0.

I have tried this.

my @arr = ();

but it always throws me a warning "Use of uninitialized value"

I do not know the size of the array before. I fill it dynamically.

I thought the above piece of code was supposed to initialize it to 0.

Can anybody tell me how to do this.

Thank you.

+5  A: 

What do you mean by "initialize an array to zero"? Arrays don't contain "zero" -- they can contain "zero elements", which is the same as "an empty list". Or, you could have an array with one element, where that element is a zero: my @array = (0);

my @array = (); should work just fine -- it allocates a new array called @array, and then assigns it the empty list, (). Note that this is identical to simply saying my @array;, since the initial value of a new array is the empty list anyway.

Are you sure you are getting an error from this line, and not somewhere else in your code? Ensure you have use strict; use warnings; in your module or script, and check the line number of the error you get. (Posting some contextual code here might help, too.)

Ether
Sorry. Yes. I want to initialise the elements of the array to 0.So that I can use the array as some sort of counter for each index. Please refer above comment for exact problem.
jerrygo
if you want to initialise an array to having the same number of elements as another array all of a specific value; do something like; my @arr2 = (0) x @arr1;
MkV
@MkV: or `my @arr2=(0) x $#arr1` as well, no?
drewk
@jerrygo: you don't need to explicitly initialize the array to all zeroes; if you do `$array[$i]++` from an empty array, things will "just work".
Ether
@drewk: no, `$#arr1` is not the same as `scalar(@arr1)`. For an array with five elements, the former is 4 and the latter is 5 (assuming you haven't altered `$[` from its default).
Ether
@Ether: you are correct. I think `scalar(@arr1)` is more obvious as well and easier to read...
drewk
@Ether: That just works fine. But the problem is I still get the warnings. I want to get rid of those. Is there no way in perl where I can initialize array elements to 0. I dont want to use hash.
jerrygo
@jerrygo: you shouldn't be getting any warnings from the code I included above. What are the warnings you see, and are you sure they are not from some other line?
Ether
+7  A: 

If I understand you, perhaps you don't need an array of zeroes; rather, you need a hash. The hash keys will be the values in the other array and the hash values will be the number of times the value exists in the other array:

use strict;
use warnings;

my @other_array = (0,0,0,1,2,2,3,3,3,4);
my %tallies;
$tallies{$_} ++ for @other_array;

print "$_ => $tallies{$_}\n" for sort {$a <=> $b} keys %tallies;    

Output:

0 => 3
1 => 1
2 => 2
3 => 3
4 => 1

To answer your specific question more directly, to create an array populated with a bunch of zeroes, you can use the technique in these two examples:

my @zeroes = (0) x 5;            # (0,0,0,0,0)

my @zeroes = (0) x @other_array; # A zero for each item in @other_array.
                                 # This works because in scalar context
                                 # an array evaluates to its size.
FM
+1: You write beautiful Perl my friend...
drewk
Thank you. Thats amazing. But still wonder, why cant I have array elements initialsed to 0. without using hash. The problem is it just works, but gives me a warning before accessing an uninitialsed value.
jerrygo
@jerrygo: are you by any chance using an old version of perl? This code above should not give you any warnings.
Ether
@jerrygo See the edited question.
FM
Thank you so much..it solved the problem. :)
jerrygo
+2  A: 

To produce the output in your comment to your post, this will do it:

use strict;
use warnings;

my @other_array = (0,0,0,1,2,2,3,3,3,4);
my @array;
my %uniqs;

$uniqs{$_}++ for @other_array;

foreach (keys %uniqs) { $array[$_]=$uniqs{$_} }

print "array[$_] = $array[$_]\n" for (0..$#array);

Output:

   array[0] = 3
   array[1] = 1
   array[2] = 2
   array[3] = 3
   array[4] = 1

This is different than your stated algorithm of producing a parallel array with zero values, but it is a more Perly way of doing it...

If you must have a parallel array that is the same size as your first array with the elements initialized to 0, this statement will dynamically do it: @array=(0) x scalar(@other_array); but really, you don't need to do that.

drewk