But wouldn't the original initilization also work?

@array1[0..5] = 1;

This seemed to populate the array just fine.

Randy W. Sims wrote:

On 12/25/2003 2:51 AM, Duong Nguyen wrote:

From: Randy W. Sims

>


On 12/25/2003 12:59 AM, Duong Nguyen wrote:
> Hello everyone,
> > Sorry for the spam, I am new to Perl and am having a hard time manipulating arrays. Below is the sample code that I am working on:
> > @array1[0..5] = 1;
> @total[0] = 0;
> > for($i=0; $i<4; $i++)
> {
> if($i == 0)
> {
> @total[$i] = @array1[$i];
> print @total[$i];
> }
> else
> {
> $j = $i - 1;
> @total[$i] = @total[$j] + @array1[$i];
> print @total[$i];
> }
> }
> > This code would work in C/C++, but with Perl, instead of adding up the previous values, the array "Total" seems to treat the integer value as a string and join them together. So instead of ending up with 4 as my resulting total, I instead get 1111. I know this is a rather dumb question, but any help would be GREATLY appreciated. Thanks in advance.


hint #1: Add the following to the beginning of your program

  use strict;
  use warnings;

hint #2: Do you know the difference between '@total[$i]' and '$total[$i]' ?

>
> Every time I use "strict" my program goes all crazy on me with the global and local variables. So instead, I just comment that out.


That craziness suggests opportunities to learn. It really is easier to debug when you get in the habit of using those pragmas. It prevents a lot of headache.

> The difference between '@total[$i]' and "@total[$i]" is that with the single quote, what u have there is what gets printed out. With double quote, the element of the array gets printed out.

No, look at the first character '@total[0]' vs '$total[0]' (@ vs $).
The first refers to an array (slice) with one element. The second refers to one element of an array.


BTW, wrt your other reply to the group, the initialization syntax you want is:

@array[0..5] = (1) x 6;

Regards,
Randy.





--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to