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 = <key1 key2 key3 key4> Z=> <val1 val2 val3 val4>

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.

On Wednesday, March 01, 2017 02:17 PM, Todd Chester wrote:
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.

<code>
#!/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";
</code>

./LoopIndexTest.pl6
loop of @x
     Line no. <1> has an index of <0> and a value of <a>
     Line no. <2> has an index of <1> and a value of <b>
     Line no. <3> has an index of <2> and a value of <c>
     Line no. <4> has an index of <3> and a value of <d>

loop of %y
     %y has an key of <cc> and a value of <CC>
     %y has an key of <dd> and a value of <DD>
     %y has an key of <bb> and a value of <BB>
     %y has an key of <aa> and a value of <AA>


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 = <aa bb cc dd> Z=> 'AA'..*;
> #{aa => AA, bb => AB, cc => AC, dd => AD}
>
> But if the contents matter, how about,
> my %y = <aa bb cc dd> Z=> <AA BB CC DD>;
> #{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

Reply via email to