Re: How do a pe-salt an array inside an object?
@ToddandMargo wrote: 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. I definitely agree with you, so let's use what we know of Raku to our advantage. A hash structure would work well, here with the Array-Indexes-to-Use as keys: class AA { has @.I; submethod BUILD (:$data) { @!I[.key] = .value for $data[] } method new (*@data) { self.bless( data => @data ) } } You can now do the following: AA.new(2 => 'b', 4 => 66, 9 => 'apples') Which makes for a nice initializer! - X
Re: How do a pe-salt an array inside an object?
On 7/6/21 1:23 PM, Bruce Gray wrote: On Jul 6, 2021, at 3:10 PM, Tom Browder wrote: On Tue, Jul 6, 2021 at 14:53 ToddAndMargo via perl6-users wrote: ... writing out 2000 skips is not practical. Todd, why don't you tell us what you're really trying to do, i.e., what is your use case? Maybe the experts can suggest a better class design. -Tom I also would prefer seeing a use case. In the meantime: my @will_become_I_soon; @will_become_I_soon[400] = "four hundred"; @will_become_I_soon[2000] = "two thousand"; my $CC = AA.new( I => @will_become_I_soon ); Hmmm. 1002 ways! or my $CC = AA.new( I => [ flat( Str xx 400, "four hundred", Str xx 2000-400-1, "two thousand" ) ] ); Sweet! Thank you! No use case. I am just in learning mode on the different ways to pre-salt an object on creation.
Re: How do a pe-salt an array inside an object?
On 7/6/21 1:10 PM, Tom Browder wrote: On Tue, Jul 6, 2021 at 14:53 ToddAndMargo via perl6-users mailto:perl6-users@perl.org>> wrote: ... writing out 2000 skips is not practical. Todd, why don't you tell us what you're really trying to do, i.e., what is your use case? Maybe the experts can suggest a better class design. -Tom Hi Tom, I am only trying to write down the various ways to pre-salting an object on creation. In practice, if I can not figure out how to pre-salt in the declaration, I just put what I want into what I want right under the object declaration. Raku is like super glue: 1001 ways to do everything. I am just in learning mode here. I adore hashes, but have found myself switching over to custom classes wherever I use to use hashes. Custom classes are a ton more versatile and I don't have to constantly look up their syntax -T
Re: How do a pe-salt an array inside an object?
> On Jul 6, 2021, at 3:10 PM, Tom Browder wrote: > > On Tue, Jul 6, 2021 at 14:53 ToddAndMargo via perl6-users > wrote: > ... > writing out 2000 skips is not practical. > > Todd, why don't you tell us what you're really trying to do, i.e., what is > your use case? Maybe the experts can suggest a better class design. > > -Tom I also would prefer seeing a use case. In the meantime: my @will_become_I_soon; @will_become_I_soon[400] = "four hundred"; @will_become_I_soon[2000] = "two thousand"; my $CC = AA.new( I => @will_become_I_soon ); or my $CC = AA.new( I => [ flat( Str xx 400, "four hundred", Str xx 2000-400-1, "two thousand" ) ] ); -- Hope this helps, Bruce Gray (Util of PerlMonks)
Re: How do a pe-salt an array inside an object?
On Tue, Jul 6, 2021 at 14:53 ToddAndMargo via perl6-users < perl6-users@perl.org> wrote: ... > writing out 2000 skips is not practical. Todd, why don't you tell us what you're really trying to do, i.e., what is your use case? Maybe the experts can suggest a better class design. -Tom
Re: How do a pe-salt an array inside an object?
On Tue, Jul 6, 2021 at 10:42 AM ToddAndMargo via perl6-users 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
Re: How do a pe-salt an array inside an object?
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. On Tue, Jul 6, 2021 at 10:42 AM ToddAndMargo via perl6-users < 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 > > > > > -- Fernando Santagata
Re: How do a pe-salt an array inside an object?
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
Re: How do a pe-salt an array inside an object?
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 On Tue, Jul 6, 2021 at 9:39 AM ToddAndMargo via perl6-users < perl6-users@perl.org> wrote: > >> On Tue, 6 Jul 2021 at 10:55, ToddAndMargo via perl6-users > >> mailto:perl6-users@perl.org>> wrote: > >> > >> Hi All, > >> > >> On creation, I would like to salt some of the > >> values inside an object when the variable > >> inside is an array: > >> > >> > >> First a concept test: > >> $ p6 'class AA { has Str @.I is rw; }; my $CC= AA.new; $CC.I[1] = > >> "def"; > >> say $CC.I[1];' > >> > >> def > >> > >> > >> Now for the pre-salt test > >> $ p6 'class AA { has Str @.I is rw; }; my $CC = AA.new( I[1] => > >> "abc" ); > >> say $CC.I[1];' > >> > >> ===SORRY!=== Error while compiling -e > >> Undeclared name: > >> I used at line 1 > >> > >> > >> What am I doing wrong? > >> > >> Many thanks, > >> -T > > On 7/5/21 10:45 PM, Norman Gaywood wrote: > > $ raku > > Welcome to 퐑퐚퐤퐮퐝퐨™ v2021.06. > > Implementing the 퐑퐚퐤퐮™ programming language v6.d. > > Built on MoarVM version 2021.06. > > > > To exit type 'exit' or '^D' > > > class AA { has Str @.I is rw; }; > > (AA) > > > my $CC = AA.new( I => [Str,"abc"] ); > > AA.new(I => Array[Str].new(Str, "abc")) > > > say $CC.I; > > [(Str) abc] > > > say $CC.I[1]; > > abc > > > $CC.I[0] = "zyz"; > > zyz > > > say $CC.I; > > [zyz abc] > > > > > > > Hi Norman, > > 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 > > -- Fernando Santagata
Re: How do a pe-salt an array inside an object?
On Tue, 6 Jul 2021 at 10:55, ToddAndMargo via perl6-users mailto:perl6-users@perl.org>> wrote: Hi All, On creation, I would like to salt some of the values inside an object when the variable inside is an array: First a concept test: $ p6 'class AA { has Str @.I is rw; }; my $CC= AA.new; $CC.I[1] = "def"; say $CC.I[1];' def Now for the pre-salt test $ p6 'class AA { has Str @.I is rw; }; my $CC = AA.new( I[1] => "abc" ); say $CC.I[1];' ===SORRY!=== Error while compiling -e Undeclared name: I used at line 1 What am I doing wrong? Many thanks, -T On 7/5/21 10:45 PM, Norman Gaywood wrote: $ raku Welcome to 퐑퐚퐤퐮퐝퐨™ v2021.06. Implementing the 퐑퐚퐤퐮™ programming language v6.d. Built on MoarVM version 2021.06. To exit type 'exit' or '^D' > class AA { has Str @.I is rw; }; (AA) > my $CC = AA.new( I => [Str,"abc"] ); AA.new(I => Array[Str].new(Str, "abc")) > say $CC.I; [(Str) abc] > say $CC.I[1]; abc > $CC.I[0] = "zyz"; zyz > say $CC.I; [zyz abc] > Hi Norman, 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
Re: How do a pe-salt an array inside an object?
$ raku Welcome to 퐑퐚퐤퐮퐝퐨™ v2021.06. Implementing the 퐑퐚퐤퐮™ programming language v6.d. Built on MoarVM version 2021.06. To exit type 'exit' or '^D' > class AA { has Str @.I is rw; }; (AA) > my $CC = AA.new( I => [Str,"abc"] ); AA.new(I => Array[Str].new(Str, "abc")) > say $CC.I; [(Str) abc] > say $CC.I[1]; abc > $CC.I[0] = "zyz"; zyz > say $CC.I; [zyz abc] > On Tue, 6 Jul 2021 at 10:55, ToddAndMargo via perl6-users < perl6-users@perl.org> wrote: > Hi All, > > On creation, I would like to salt some of the > values inside an object when the variable > inside is an array: > > > First a concept test: > $ p6 'class AA { has Str @.I is rw; }; my $CC= AA.new; $CC.I[1] = "def"; > say $CC.I[1];' > > def > > > Now for the pre-salt test > $ p6 'class AA { has Str @.I is rw; }; my $CC = AA.new( I[1] => "abc" ); > say $CC.I[1];' > > ===SORRY!=== Error while compiling -e > Undeclared name: > I used at line 1 > > > What am I doing wrong? > > Many thanks, > -T > > -- > > If I had a dime every time I didn't know > what was going on, I'd be like, "Why is > everyone giving me all these dimes?" > > -- Norman Gaywood, Computer Systems Officer School of Science and Technology University of New England Armidale NSW 2351, Australia ngayw...@une.edu.au http://turing.une.edu.au/~ngaywood Phone: +61 (0)2 6773 2412 Mobile: +61 (0)4 7862 0062 Please avoid sending me Word or Power Point attachments. See http://www.gnu.org/philosophy/no-word-attachments.html
How do a pe-salt an array inside an object?
Hi All, On creation, I would like to salt some of the values inside an object when the variable inside is an array: First a concept test: $ p6 'class AA { has Str @.I is rw; }; my $CC= AA.new; $CC.I[1] = "def"; say $CC.I[1];' def Now for the pre-salt test $ p6 'class AA { has Str @.I is rw; }; my $CC = AA.new( I[1] => "abc" ); say $CC.I[1];' ===SORRY!=== Error while compiling -e Undeclared name: I used at line 1 What am I doing wrong? Many thanks, -T -- If I had a dime every time I didn't know what was going on, I'd be like, "Why is everyone giving me all these dimes?"