Re: for loop index question
On 02/28/2017 11:06 PM, Andrew Kirkpatrick wrote: The zip operator in this case takes two sequences and interleaves them into a single sequence. It might be useful if you have handy or can generate a list of keys and a list of values you want to put together in pairs using => to create a hash table. Your explicit approach makes sense for readability - it can aid comprehension if a fake address looks like a real one. Thank you!
Re: for loop index question
On 03/01/2017 12:45 AM, Richard Hainsworth wrote: Todd, As Andrew explained Z takes two arrays and an operator, eg. =>, or +, and then 'runs' the operator on the elements of the two lists. Here, you defined @x as a list of strings. I defined two lists, one of keys and one of values. Then I zipped the two together with the => operator that associates keys with values. Personally, I find %h = Z=> as easier to read. I am happy not to have () and "" and ',', and the problems of matching them, cluttering the line. I think that the fewer the characters there are, the clearer it is to see the relationships. It depends on the code, and different code is more elegant and easier to maintain when written in a different way. But that is an opinion. It seems to me that there are a number of new idioms arising in Perl6. Eventually the more elegant and computationally efficient idioms will become apparent. From time to time, I look at the code in the modules to see if there are nicer ways to express something. I also can type almost as fast I think. But having typed, I think again, and usually rewrite to make things clearer. Thank you!
Re: for loop index question
The zip operator in this case takes two sequences and interleaves them into a single sequence. It might be useful if you have handy or can generate a list of keys and a list of values you want to put together in pairs using => to create a hash table. Your explicit approach makes sense for readability - it can aid comprehension if a fake address looks like a real one. On 1 March 2017 at 16:47, Todd Chesterwrote: >> On Wednesday, March 01, 2017 01:01 PM, Todd Chester wrote: >>> >>> Hi All, >>> >>> And it even gets more interesting. >>> >>> It even works with Associative Arrays (Hashes), which I adore! >>> >>> Interesting how associative arrays don't print in order that they >>> were entered into the array. >>> >>> >>> #!/usr/bin/perl6 >>> >>> my @x = ( "a", "b", "c", "d" ); >>> >>> print "loop of \@x\n"; >>> for @x.kv -> $index, $value { >>>print " Line no. <" ~ ($index + 1) ~ >>>"> has an index of <$index> and a value of <$value>\n"; } >>> print "\n"; >>> >>> >>> my %y = ( 'aa'=>"AA", 'bb'=> "BB", 'cc'=>"CC", 'dd'=>"DD" ); >>> >>> print "loop of \%y\n"; >>> for %y.kv -> $key, $value { >>>print " \%y has an key of <$key> and a value of <$value>\n"; } >>> print "\n"; >>> >>> >>> ./LoopIndexTest.pl6 >>> loop of @x >>> Line no. <1> has an index of <0> and a value of >>> Line no. <2> has an index of <1> and a value of >>> Line no. <3> has an index of <2> and a value of >>> Line no. <4> has an index of <3> and a value of >>> >>> loop of %y >>> %y has an key of and a value of >>> %y has an key of and a value of >>> %y has an key of and a value of >>> %y has an key of and a value of > > > > On 02/28/2017 09:27 PM, Richard Hainsworth wrote: >> Instead of >> my %y = ( 'aa'=>"AA", 'bb'=> "BB", 'cc'=>"CC", 'dd'=>"DD" ); >> >> in the code below, >> >> I find the following useful for filling an associative array (when >> testing and I dont care about the contents) >> >> my %y = Z=> 'AA'..*; >> #{aa => AA, bb => AB, cc => AC, dd => AD} >> >> But if the contents matter, how about, >> my %y = Z=> ; >> #{aa => AA, bb => BB, cc => CC, dd => DD} >> >> The 'Z' is the zip operator, the '=>' links a key to its value. >> The ..* uses the power of perl6 to generate sequences from an initial >> member. >> >> Richard >> > > Hi Richard, > > I don't understand how the zipper works or > why I'd want to use it. > > my %Location = ( 'City'=>"Carson City", > 'State'=>"NV", > 'ZipCode'=>"89701", > 'Country'=>"USA" ); # just made up place > > Make sense to me and is easily maintainable. > I do not get where the zipper comes in? Or > how it would make things easier to read? > > By chance is the idea that you don't have to the assign the > values at the same time you assign the keys? > > There is extra time spent on writing the way I did it, but > I don't care. I know how to type (high school typing) and > my main goal it to make the code maintainable. (This is why I > use Top Down as well.) I can type almost as fast as I think. > > I am confused, > -T
Re: for loop index question
On Wednesday, March 01, 2017 01:01 PM, Todd Chester wrote: Hi All, And it even gets more interesting. It even works with Associative Arrays (Hashes), which I adore! Interesting how associative arrays don't print in order that they were entered into the array. #!/usr/bin/perl6 my @x = ( "a", "b", "c", "d" ); print "loop of \@x\n"; for @x.kv -> $index, $value { print " Line no. <" ~ ($index + 1) ~ "> has an index of <$index> and a value of <$value>\n"; } print "\n"; my %y = ( 'aa'=>"AA", 'bb'=> "BB", 'cc'=>"CC", 'dd'=>"DD" ); print "loop of \%y\n"; for %y.kv -> $key, $value { print " \%y has an key of <$key> and a value of <$value>\n"; } print "\n"; ./LoopIndexTest.pl6 loop of @x Line no. <1> has an index of <0> and a value of Line no. <2> has an index of <1> and a value of Line no. <3> has an index of <2> and a value of Line no. <4> has an index of <3> and a value of loop of %y %y has an key of and a value of %y has an key of and a value of %y has an key of and a value of %y has an key of and a value of On 02/28/2017 09:27 PM, Richard Hainsworth wrote: > Instead of > my %y = ( 'aa'=>"AA", 'bb'=> "BB", 'cc'=>"CC", 'dd'=>"DD" ); > > in the code below, > > I find the following useful for filling an associative array (when > testing and I dont care about the contents) > > my %y = Z=> 'AA'..*; > #{aa => AA, bb => AB, cc => AC, dd => AD} > > But if the contents matter, how about, > my %y = Z=> ; > #{aa => AA, bb => BB, cc => CC, dd => DD} > > The 'Z' is the zip operator, the '=>' links a key to its value. > The ..* uses the power of perl6 to generate sequences from an initial > member. > > Richard > Hi Richard, I don't understand how the zipper works or why I'd want to use it. my %Location = ( 'City'=>"Carson City", 'State'=>"NV", 'ZipCode'=>"89701", 'Country'=>"USA" ); # just made up place Make sense to me and is easily maintainable. I do not get where the zipper comes in? Or how it would make things easier to read? By chance is the idea that you don't have to the assign the values at the same time you assign the keys? There is extra time spent on writing the way I did it, but I don't care. I know how to type (high school typing) and my main goal it to make the code maintainable. (This is why I use Top Down as well.) I can type almost as fast as I think. I am confused, -T
Re: for loop index question
Hi All, And it even gets more interesting. It even works with Associative Arrays (Hashes), which I adore! Interesting how associative arrays don't print in order that they were entered into the array. #!/usr/bin/perl6 my @x = ( "a", "b", "c", "d" ); print "loop of \@x\n"; for @x.kv -> $index, $value { print " Line no. <" ~ ($index + 1) ~ "> has an index of <$index> and a value of <$value>\n"; } print "\n"; my %y = ( 'aa'=>"AA", 'bb'=> "BB", 'cc'=>"CC", 'dd'=>"DD" ); print "loop of \%y\n"; for %y.kv -> $key, $value { print " \%y has an key of <$key> and a value of <$value>\n"; } print "\n"; ./LoopIndexTest.pl6 loop of @x Line no. <1> has an index of <0> and a value of Line no. <2> has an index of <1> and a value of Line no. <3> has an index of <2> and a value of Line no. <4> has an index of <3> and a value of loop of %y %y has an key of and a value of %y has an key of and a value of %y has an key of and a value of %y has an key of and a value of
Re: for loop index question
On 02/28/2017 01:30 PM, yary wrote: Maybe using placeholder variables was a bit too much (those variables with the ^ twigil) for @a.kv -> $k, $v { say "Value $v has index $k" } Value g has index 0 Value h has index 1 Value i has index 2 Value j has index 3 Value k has index 4 Hi Yary, Thank you! -T
Re: for loop index question
On 02/28/2017 01:20 PM, ToddAndMargo wrote: Hi All, There are times when I want to know th4e index of an array when I am in a "for @array" loop. I can do it with a variable outside the for loop and increment it, but I would line to know know if there is a way to incorporate it in the loop command. This is my long winded way of doing it in Perl 5: while (my ($Index, $Element) = each ( @Sorted_List ) ) { do something } Many thanks, -T Hi All, Thank you all! Made me look up "kv": https://docs.perl6.org/routine/kv Returns an interleaved sequence of indexes and values. For example .kv; # (0 a 1 b 2 c) I will be copying this down in my list of examples. Note that I made the point that Perl starts counting at zero not one. -T $ cat ./LoopIndexTest.pl6 #!/usr/bin/perl6 my @x = ( "a", "b", "c", "d" ); for @x.kv -> $index, $value { print " Line no. <" ~ ($index + 1) ~ "> has an index of <$index> and a value of <$value>\n"; } $ ./LoopIndexTest.pl6 Line no. <1> has an index of <0> and a value of Line no. <2> has an index of <1> and a value of Line no. <3> has an index of <2> and a value of Line no. <4> has an index of <3> and a value of
Re: for loop index question
On Tue, Feb 28, 2017 at 01:20:47PM -0800, ToddAndMargo wrote: Hi All, There are times when I want to know th4e index of an array when I am in a "for @array" loop. I can do it with a variable outside the for loop and increment it, but I would line to know know if there is a way to incorporate it in the loop command. This is my long winded way of doing it in Perl 5: while (my ($Index, $Element) = each ( @Sorted_List ) ) { do something } Many thanks, -T On 02/28/2017 01:23 PM, Patrick R. Michaud wrote: > I think the canonical Perl 6 answer is: > > for @array.kv -> $index, $value { do something } > > Pm Thank you!
Re: for loop index question
Maybe using placeholder variables was a bit too much (those variables with the ^ twigil) > for @a.kv -> $k, $v { say "Value $v has index $k" } Value g has index 0 Value h has index 1 Value i has index 2 Value j has index 3 Value k has index 4
Re: for loop index question
> my @a = ( 'g' .. 'k' ) [g h i j k] > @a.kv (0 g 1 h 2 i 3 j 4 k) > for @a.kv { say "Value $^v has index $^i" } Value g has index 0 Value h has index 1 Value i has index 2 Value j has index 3 Value k has index 4
Re: for loop index question
> On 28 Feb 2017, at 22:20, ToddAndMargowrote: > > Hi All, > > There are times when I want to know th4e index of an array > when I am in a "for @array" loop. I can do it with a > variable outside the for loop and increment it, but > I would line to know know if there is a way to incorporate > it in the loop command. > > This is my long winded way of doing it in Perl 5: > > while (my ($Index, $Element) = each ( @Sorted_List ) ) { do something } use .kv: $ 6 'my @a = ; for @a.kv -> $index,$value { dd $index, $value }' Int $index = 0 Str $value = "a" Int $index = 1 Str $value = "b" Int $index = 2 Str $value = “c" Mind you, you can actually chain that with your sort: $ 6 'my @a = ; for @a.sort.kv -> $index,$value { dd $index, $value }' Int $index = 0 Str $value = "a" Int $index = 1 Str $value = "b" Int $index = 2 Str $value = “c" HTH, Liz
Re: for loop index question
I think the canonical Perl 6 answer is: for @array.kv -> $index, $value { do something } Pm On Tue, Feb 28, 2017 at 01:20:47PM -0800, ToddAndMargo wrote: > Hi All, > > There are times when I want to know th4e index of an array > when I am in a "for @array" loop. I can do it with a > variable outside the for loop and increment it, but > I would line to know know if there is a way to incorporate > it in the loop command. > > This is my long winded way of doing it in Perl 5: > > while (my ($Index, $Element) = each ( @Sorted_List ) ) { do something } > > > Many thanks, > -T > > > > -- > ~~ > Computers are like air conditioners. > They malfunction when you open windows > ~~