Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

scalar/array contexts in perl5 vs. perl6

0 views
Skip to first unread message

Mike Li

unread,
Dec 4, 2005, 1:10:44 PM12/4/05
to perl6-c...@perl.org, perl6-l...@perl.org
what is a good translation of the following C into perl6?

<code>
#include <stdio.h>

void print(int y[])
{
int ii;

for (ii = 0; 9 > ii; ++ii)
{
printf("%d ", y[ii]);
}

printf("\n");
}

int main()
{
int x = 0; int y[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; y[x++]++; /* line
that matters */

printf("%d\n", x);
print(y);

return 0;
}
</code>

in perl5, i would've written something like:

<code>
my $x = 0; my @y = 1..9; @y[$x++]++; print "$x\n"; print "@y\n"
</code>

but in perl6, the '@' sigil always means list context, so should i
write the following?

<code>
my $x = 0; my @y = 1..9; ${@y[${$x++}]}++; print "$x\n"; print "@y\n"
</code>

-xgl

Jonathan Scott Duff

unread,
Dec 5, 2005, 9:51:49 AM12/5/05
to Mike Li, perl6-c...@perl.org, perl6-l...@perl.org
On Sun, Dec 04, 2005 at 01:10:44PM -0500, Mike Li wrote:
> what is a good translation of the following C into perl6?
[snip]

>
> in perl5, i would've written something like:
>
> <code>
> my $x = 0; my @y = 1..9; @y[$x++]++; print "$x\n"; print "@y\n"
> </code>
>
> but in perl6, the '@' sigil always means list context,

Um ... do you mean s/perl6/perl5/ above? In perl6 the @ sigil does
not always mean list context; it just means that you're dealing with
an array somehow.

Your code above is practically perl6 already. The only problem with
it that I see is that you need to use @y[] to interpolate an array
into a string:

my $x = 0; my @y = 1..9; @y[$x++]++; print "$x\n"; print "@y[]\n"

-Scott
--
Jonathan Scott Duff
du...@pobox.com

0 new messages