At 12:39 PM 8/16/00 +1000, Jeremy Howard wrote:
>Mark Cogan wrote:
> > At 05:47 PM 8/15/00 -0600, Nathan Torkington wrote:
> > >Jeremy Howard writes:
> > > > @result = @a || @b;
> > > >
> > > > Which applies '||' component-wise to elements of @a and @b, placing
>the
> > > > result in @result.
> > >
> > >*Ptui* That's not how *I* want || to behave on lists/arrays.
> > >
> > >I want
> > > @result = @a || @b;
> > >to be like:
> > > (@result = @a) or (@result = @b);
> > >
> > >That's what all my students keep expecting it to mean.
> >
> > Seconded.
> >
> > It seems obvious that @a should be the whole array @a, not an iteration
> > over its elements. If I want to iterate over @a, I should have to do so
> > explicitly, with a for() or map().
>
>It seems obvious that @a should be the whole array @a, not the size of the
>array. If I want to check the size of @a, I should have to do so explicitly,
>with scalar or $#.
>
>This is non-obvious if you think that || is a flow control statement, so
>think about * for a moment:
>
> @c = @b * @a;
But, to me at least, arrays aren't something you multiply. Multiplication
applies to numbers (scalars), and returns a scalar, so when you see
@c = @b * @a
it should be clear that something funny is going on.
And, really, what is wrong with:
@c = map {$a[$_] * $b[$_]} (0..$#a);
?
(or, for that matter:
push @c, $a[$_] * $b[$_] for (0..$#a);
)
in both cases it's clear that you're iterating over the elemnts in the array.
>It's pretty clear what working on 'the whole array' means here, I think.
I disagree. In particular, think of it from the point of view of someone
who hasn't studied computer science.
What should:
@a = defined @a;
return?
>Treating || as a special case is asking for trouble. If you want a flow
>control statement, use one:
>
> @result = @b unless @result = @a;
|| may be a suboptimal example, but I think the idea that a @-variable
without an iteration function refers to the array as a whole, and not its
elements is an intuitive one, and having array iteration magically happen
when you're not looking is dangerous.
-------------------------------------------------------------------
Mark Cogan [EMAIL PROTECTED] +1-520-881-8101
ArtToday www.arttoday.com