develooper Front page | perl.perl6.users | Postings from July 2021

Re: How do a pe-salt an array inside an object?

Thread Previous | Thread Next
From:
ToddAndMargo via perl6-users
Date:
July 6, 2021 19:53
Subject:
Re: How do a pe-salt an array inside an object?
Message ID:
de42703e-9f05-b3e1-5f55-0714ef08fb3f@zoho.com
>> On Tue, Jul 6, 2021 at 10:42 AM ToddAndMargo via perl6-users 
>> <perl6-users@perl.org <mailto:perl6-users@perl.org>> wrote:
>> 
>>      >> On Tue, Jul 6, 2021 at 9:39 AM ToddAndMargo via perl6-users
>>      >>     I am confused.
>>      >>
>>      >>     What I am after it pre-salting $CC.I  with
>>      >>          $CC.[0] = "abc"
>>      >>          $CC.[1] = "def"
>>      >>
>>      >>     with the ".new" functions when I create $CC
>>      >>
>>      >>     -T
>> 
>>     On 7/6/21 12:52 AM, Fernando Santagata wrote:
>>      > Hello,
>>      >
>>      > I think that that was exactly what Norman was trying to show.
>>     Probably
>>      > you've been mislead by the first value they assigned in their
>>     example:
>>      >
>>      >     my $CC = AA.new( I => [Str,"abc"] );
>>      >
>>      >
>>      > Here the 'Str' is the "empty" or "undefined" value, since the
>>     array was
>>      > declared as a Str array.
>>      >
>>      > Try this:
>>      >
>>      >  > class AA { has Str @.I is rw }
>>      > (AA)
>>      >  > my $aa = AA.new: I => ['a','b']
>>      > AA.new(I => Array[Str].new("a", "b"))
>>      >  > say $aa.I[0]
>>      > a
>>      >  > say $aa.I[1]
>>      > b
>>      >
>>      > or this
>>      >
>>      >  > my $bb = AA.new: I => 'a'
>>      > AA.new(I => Array[Str].new("a"))
>>      >  > $bb.I[0]
>>      > a
>>      >
>> 
>>     Hi Fernando,
>> 
>>     Thank you!   Now I understand the syntax better.
>> 
>>     $ p6 'class AA { has Str @.I is rw; };
>>             my $CC = AA.new( I => ["abc","def"] );
>>             say $CC.I[1];'
>>     def
>> 
>> 
>>     Follow up question. Since Raku allows me to assign
>>     elements to an array in non sequential order:
>> 
>>           $ p6 'my @x; @x[4]=44; say @x;'
>>           [(Any) (Any) (Any) (Any) 44]
>> 
>>     How to I modify
>>            my $CC = AA.new( I => ["abc","def"] );
>>     such that I can place values in non sequential order?
>> 
>>     For example, I wanted to pre-salt
>>            $CC.I[4] = "four"
>>            $CC.I[2] = "two"
>> 
>>     Many thanks,
>>     -T

On 7/6/21 2:06 AM, Fernando Santagata wrote:
> Hi,
> for your last question, let's use again what Norman showed you earlier:
> 
>  > class AA { has Str @.I is rw }
> (AA)
>  > my $CC = AA.new(I => [Str, Str, 'two', Str, 'four'])
> AA.new(I => Array[Str].new(Str, Str, "two", Str, "four"))
>  > say $CC.I[0]
> (Str)
>  > say $CC.I[2]
> two
>  > say $CC.I[4]
> four
> 
> In this case undefined values are initialized by their own base class. 
> Since @.I was declared as a Str array, then Norman used the base class 
> 'Str' as initialization value.


Now I understand.  You are using Str as a
     skip, skip, drop 2, skip, drop 4
This is a sequential workaround.

Also I was confused as I though Str was only
a type declaration and could be used this way.

What I would like to see is direct access, without the
skips.

In other words, the pre-salt equivalent of

class AA { has Str @.I is rw };
my $CC = AA.new;
$CC.I[400]  = "four hundred"
$CC.I[2000] = "two thousand"

writing out 2000 skips is not practical.

-T



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