develooper Front page | perl.perl6.language.data | Postings from September 2000

Re: RFC 207 (v1) Array: Efficient Array Loops

Thread Previous | Thread Next
From:
Jeremy Howard
Date:
September 11, 2000 14:50
Subject:
Re: RFC 207 (v1) Array: Efficient Array Loops
Message ID:
001901c01c3a$7597afd0$d5252ccb@optimaldecisions.com
Buddha Buck wrote:
> At 12:00 AM 9/12/00 +1100, Jeremy Howard wrote:
>  >c.soeller@auckland.ac.nz wrote:
>  >> Reading through the examples left me wondering about some
>  >> technicalities:
>  >>
>  >> >   @t[|i;|j] = @a[|j;|i];  # transpose 2-d @a
>  >>
>  >> Written like this it would require that @a is exact 2-dim, i.e. it
would
>  >> not just swap the first two dims of any n-dim array? I suppose if I'd
>  >> want that I'd write
>  >>
>  >>     @t[|i;|j;] = @a[|j;|i;]; # trailing ';' implies there might be
>  >> trailing dims
>  >>
>  >Not necessary. Since arrays support all the syntax of a plain old list
of
>  >lists, and the |i syntax just creates an implicit loop, the example
quoted
>  >from the RFC will work with other sized arrays. In fact, if it was only
2d,
>  >it would more properly be:
>  >
>  >  $t[|i;|j] = $a[|j;|i];  # transpose 2-d @a
>
<...>
>
> While something like "$t[|i;|j] = $a[|j;|i];" looks clearly like
> scalar-to-scaler assignment of 2-D array elements, something like "@p =
> $a[$k;|i]*$b[|i;$k];" isn't so clear.  Multiply two scalers to get an
> array?!?  In truth, it's assigning an array to an array, so there is no
> problem except appearances.
>
I don't think:

  @p = $a[$k;|i]*$b[|i;$k];

is meaningful. I would have thought that the iterator indexes must appear on
_both_ sides of an expression:

  $p[|i] = $a[$k;|i]*$b[|i;$k];

unless using a reduction:

  $sum = reduce ^_ + ^_, $a[$k;|i]*$b[|i;$k];

That way it is easy to conceptualise the implicit loop that is being
created. However, there is an issue with the width of the implicit loop. In
the above expression I want reduce() to be sent the array of values
$a[$k;|i]*$b[|i;$k]. However, in:

  $sum[|i] = reduce ^_ + ^_, $a[$k;|i]*$b[|i;$k];

I want reduce to be sent the list at each iteration of $a[$k;|i]*$b[|i;$k],
where @a and @b and both 3d arrays.

I have no idea how to do this...



Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About