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

xx operator

41 views
Skip to first unread message

Fagyal Csongor

unread,
Sep 28, 2006, 9:11:31 AM9/28/06
to perl6-users
Hi,

I have the following code:

class MyStupidString {
method repeatit(Str $string, Int $repeat) {
say $string xx $repeat;
my $add = $string xx $repeat;
say $add;
}

};

my $obj = MyStupidString.new;
$obj.repeatit("---",10);

Interestingly, it says:
> pugs test2.p6
------------------------------
--- --- --- --- --- --- --- --- --- ---


What am I misunderstanding here? Or is this a Pugs bug?

I am using pugs 6.2.12

- Fagzal

A. Pagaltzis

unread,
Sep 28, 2006, 9:21:35 AM9/28/06
to perl6-users
> I have the following code:
>
> class MyStupidString {
> method repeatit(Str $string, Int $repeat) {
> say $string xx $repeat;
> my $add = $string xx $repeat;
> say $add;
> }
>
> };
>
> my $obj = MyStupidString.new;
> $obj.repeatit("---",10);
>
>
>
> Interestingly, it says:
> > pugs test2.p6
> ------------------------------
> --- --- --- --- --- --- --- --- --- ---
>
>
> What am I misunderstanding here?

The `xx` operator. That's list-repeat, not string-repeat.

In Perl 5 terms, the code you wrote is:

sub repeatit {
my ( $string, $repeat ) = @_;
my @res = ( $string ) x $repeat;
print @res;
my $add = "@res";
print $add;
}

which should make it obvious what is going on.
--
GMX DSL-Flatrate 0,- Euro* - Überall, wo DSL verfügbar ist!
NEU: Jetzt bis zu 16.000 kBit/s! http://www.gmx.net/de/go/dsl

Juerd

unread,
Sep 28, 2006, 9:23:38 AM9/28/06
to perl6...@perl.org
Fagyal Csongor skribis 2006-09-28 15:11 (+0200):
> say $string xx $repeat;

List context.

> my $add = $string xx $repeat;

Item context, for a list repetition operator. Doesn't really make sense,
and I think a warning or error message would be more appropriate.

I think you meant either:

my @add = $string xx $repeat;

or:

my $add = $string x $repeat;

Or perhaps:

my $add = [ $string xx $repeat ];
# This is what your current code does, but I think it's best if Perl
# enforced that you be explicit about the [].
--
korajn salutojn,

juerd waalboer: perl hacker <ju...@juerd.nl> <http://juerd.nl/sig>
convolution: ict solutions and consultancy <sa...@convolution.nl>

Fagyal Csongor

unread,
Sep 28, 2006, 10:03:04 AM9/28/06
to perl6-users
A. Pagaltzis wrote:

>>I have the following code:
>>
>>class MyStupidString {
>> method repeatit(Str $string, Int $repeat) {
>> say $string xx $repeat;
>> my $add = $string xx $repeat;
>> say $add;
>> }
>>
>>};
>>
>>my $obj = MyStupidString.new;
>>$obj.repeatit("---",10);
>>
>>
>>
>>Interestingly, it says:
>> > pugs test2.p6
>>------------------------------
>>--- --- --- --- --- --- --- --- --- ---
>>
>>
>>What am I misunderstanding here?
>>
>>
>
>The `xx` operator. That's list-repeat, not string-repeat.
>
>In Perl 5 terms, the code you wrote is:
>
> sub repeatit {
> my ( $string, $repeat ) = @_;
> my @res = ( $string ) x $repeat;
> print @res;
> my $add = "@res";
> print $add;
> }
>
>which should make it obvious what is going on.
>
>

Thank you and Juerd.

It was indeed a misunderstanding, I though "x" changed to "xx" in Perl6
for some reasons...


Now is the first time I write some Perl6 code that I actually run, too
:) It is *really* enjoyable. Feels like "this is how Perl should really
look like", and it is so DWIM I can hardly believe it. Kudos to all who
helped to make it happen!

- Fagzal

0 new messages