Dan Sugalski wrote:
> At 09:40 PM 4/6/2001 +0100, Richard Proctor wrote:
> >On Fri 06 Apr, Dan Sugalski wrote:
> > > This is, I presume, in addition to any sort of inherent DWIMmery? I
don't
> > > see any reason that:
> > >
> > > @foo[1,2] = <STDIN>;
> > >
> > > shouldn't read just two lines from that filehandle, for example, nor
why
> > >
> >
> >Fair enough
> >
> > > @bar = @foo * 12;
> > >
> > > shouldn't assign to @bar all the elements of @foo multiplied by 12.
(Though
> > > others might, of course)
> >
> >Reasonable, but what should
> >
> > @bar = @foo x 2;
> >
> >do? Repeat @foo twice or repeat each element twice? (its current
behaviour
> >is less than useless, other than for JAPHs)
>
> I'd go for repeat every element twice.
So would I:
http://dev.perl.org/rfc/82.html
...which includes some other examples:
<snip>
=head1 EXAMPLES
=head2 Text processing
If @first_names contains a list of peoples first names, and @surnames
contains their surnames, this creates a new list that concatenates the
elements of the two lists:
@full_names = @first_names . @surnames;
To quote a number of lines of a message by prefixing them all with '> ':
@quoted_lines = '> ' . @raw_lines;
To create a histogram for a list of scores:
@people = ('adam', 'eve ', 'bob ');
@scores = (7,9,5); # Score for each person
@histogram = '#' x @scores; # Returns ('xxxxxxx','xxxxxxxxx','xxxxx')
print join("\n", @people . ' ' . @histogram);
adam xxxxxxx
eve xxxxxxxxx
bob xxxxx
=head2 Number crunching
This snippet multiplies the absolute values of three arrays together and
sums the results, in a very efficient way:
@b = (1,2,3);
@c = (2,4,6);
@d = (-2,-4,-6);
$sum = reduce ^_+^_, abs(@b * @c + @d);
Lists can be reordered or sliced with list generation functions (RFC 81)
allowing flexible data manipulation:
@a = (3,6,9);
@reverse = (3..1);
@b = @a * @a[@rev]; # (3*9, 6*6, 9*3) = (27,36,27)
Slicing plus array operations makes matrix algebra easy:
@a = (1,2,3,
2,4,6,
3,6,9);
@column1of3 = (1..7:3); # (1,4,7) - every 3rd elem from 1 to 7
@row1of3 = (1..3); # (1,2,3)
$sum_col1_by_row1 =
sum ( @a[@column1of3] * @a[@row1of3] ); # (1*1+2*2+3*3)=14
</snip>