Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

easy way to initialize an array

Status
Not open for further replies.

bobbybobbertson

Programmer
Nov 9, 2001
119
US
is there an easier way than the following to initialize an array to all zeros.


$cntr=0;
while ($cntr++ < 24) {
$array[$cntr]=0;
}
 
YES, use the repetition operator x:
Code:
$numElems = 24; 
@array = (0) x $numElems;
or, if the array already exists and you want to set all it's elements to 0:
Code:
@array = (0) x @array;
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Some alternative methods:
Code:
for ( 0..23 ) { $arr[$_] = 0 };

for ( 0..23 ) { push @arr, 0 };

@arr[ 0..23 ] = map { 0 } ( 0..23 );
I'm sure there's many more :) Cheers, Neil
 
I may be wrong on this, but

while ($cntr++ < 24) {

will increment $cntr to 1 before the first

$array[$cntr]=0;

thereby leaving you with a [0] element that is NOT initialized to 0. I'm not sure why you need to have all elements initialized to 0, but if you do, others have given good examples of how to.

HTH.
Hardy Merrill
Mission Critical Linux, Inc.
 
You're right Hardy! I missed that! Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
do you really need it initialized?

You can play around in an array at will,

Code:
#!/usr/local/bin/perl
$array[3] = '4';
foreach (@array) { print &quot;Element: $_\n&quot;; }

$array[1]++;
foreach (@array) { print &quot;Element: $_\n&quot;; }

That is one of the things I like about Perl vs other languages that make you specify the size of an array before you use it.

'just my $.02 If you are new to Tek-Tips, please use descriptive titles, check the FAQs,
and beware the evil typo.
 
I suppose there might be cases where you want to initialize an array, particularly if you want each element to have a non-zero/non-null value. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
There are only two instances I can think of where I might want to initialize every element of an array to 0:

1) if I want to be able to test each element for true(non-zero) or false(zero)

2) if I want each element to be a count of something
Hardy Merrill
Mission Critical Linux, Inc.
 
Doesn't an uninitialized array element test false? Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
The fact that undefined variables evaluate to false in PERL is incredibly useful. I use it all the time to see if a form variable has been passed to my program. If it hasn't, or if it's null, it evaluates to false. You can also use it to see if optional parameters have been passed to a subroutine. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
The array is used in an HTML report. Thus, if there is nothing there, I want it to print a 0. Therefore I have to initialize it. Either that or before I print, I have to test a valueit then print a zero if it is unitialized.
Its easier for me just to initialize the array.

Plus, I use warnings in my programs, and if you try to print an unitialized value, you get a warning.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top