Ahhh.. Ok. I see the mistake. I've purchased Oreilly's "Learning Perl" 3rd Edition and have been steadily plugging through it. There is an example on page 45 which shows another way to populate an array. Here is one such example they give.

@numbers = 1..1e5;

So basically if you didn't want to use =(1) x 6 you could do it this way.

I had confused where the .. goes :-)

thx for the clarify



Randy W. Sims wrote:

On 12/25/2003 12:18 PM, u235sentinel wrote:

But wouldn't the original initilization also work?

@array1[0..5] = 1;


No. Think of this in terms of parallel assignment, ignoring for the moment that we are talking of arrays, the above is equivalent to:

($e0, $e1, $e2, $e3, $e4, $e5) = 1;

which is equivalent to:

($e0, $e1, $e2, $e3, $e4, $e5) = (1);

because the list on the left-hand side of the assignment operator ('='), puts the right-hand side in list context. This is then filled in by perl to become:

($e0, $e1, $e2, $e3, $e4, $e5) = (1, undef, undef, undef, undef, undef);

because the list on the right-hand side must have the same number of elements as the list on the left-hand side.


The common idiom to populate an array with a particular value is to use the times operator ('x') which reproduces its left-hand argument the number of times specified by its right-hand argument:


($e0, $e1, $e2, $e3, $e4, $e5) = (1) x 6;

which is equivelent to:

($e0, $e1, $e2, $e3, $e4, $e5) = ((1), (1), (1), (1), (1), (1));

which then gets flatened to:

($e0, $e1, $e2, $e3, $e4, $e5) = (1, 1, 1, 1, 1, 1);


With an array slice, perl basically turns the original:


@array1[0..5] = 1;

into something like:

($array1[0], $array1[1], $array1[2], $array1[3], $array1[4], $array1[5]) = (1, undef, undef, undef, undef, undef);

in the same manner as the first example above.


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