> Every time $ shows up, it is a different scalar.

Ah ... I was mistakenly thinking it was akin to $_ etc, where you could just 
use it for "free" but it persisted as any var would. So, in:
 raku -e 'for <AA NN> -> $alpha {  for (1..14) { say (state $ = $alpha)++; }  }

it's the "state" that keeping it around, but only ... arrgh! I'm going to have 
to read
https://docs.raku.org/language/variables#The_state_declarator

a few more times ... $ normally doesn't need state, but if you want the 
assignment to happen just once (as above), you need to explicitly add the 
state.  Without an assignment:
 raku -e 'for <AA NN> -> $alpha {  for (1..14) { say  $++; }  }'   # 0-13 0-13
 raku -e 'for <AA NN> -> $alpha {  for (1..14) { say state $++; }  }'  # 0-13 
0-13

Those the same, but
 raku -e 'for <AA NN> -> $alpha {  for (1..14) { say ( $ = $alpha)++; }  }
just shows AA and NN 14 times.  Because the inner for loops block is being 
"rerun", the state-liness of $ works, but when the outer loop loops (from AA to 
NN) the inner block and the inner version of $ go away.
 raku -e 'for <AA NN> -> $alpha {  for (1..14) { say (state $ = $alpha)++; say 
"d2: " ~ ++$ }  }'
AA
d2: 1
AB
d2: 2
...

By Geoffrey, I think I almost have it!

Thanks!



________________________________
From: yary <not....@gmail.com>
Sent: Tuesday, September 1, 2020 6:16 PM
To: Andy Bach <andy_b...@wiwb.uscourts.gov>
Cc: William Michels <w...@caa.columbia.edu>; perl6-users <perl6-us...@perl.org>
Subject: Re: print particular lines question

Every time $ shows up, it is a different scalar.

$=1; say $;

is similar to

my $anonONE=1; say $anonTWO;

thus they are very limited use

-y


On Tue, Sep 1, 2020 at 3:55 PM Andy Bach 
<andy_b...@wiwb.uscourts.gov<mailto:andy_b...@wiwb.uscourts.gov>> wrote:
> My first clue that something is amiss is in your third line of code when the  
> return skips "AA" and starts "AB, AC, AD....". That suggests to me that the 
> two step assign/printf call is playing havoc with the $ anonymous variable

Missed that about the missing AA - does the same thing  with a named var, 
though:
 raku -e 'for <AA NN> -> $alpha {  for (1..14) { (state $sv = $alpha)++;  
printf("d: %s\n", $sv ) } }'
d: AB
d: AC

So
 raku -e 'for <AA NN> -> $alpha {  for (1..14) { say (state $sv = $alpha)++;  
printf("d: %s\n", $sv ) } }'
AA
d: AB
AB
d: AC

Ah, the increment happens the initial assignment.
$sv = "AA";
$sv++;
print $sv;  # AB

but the
say (state $sv = $alpha)++

says the result of the assignment, then the increment.  My confusion was more 
about my inability to use "$" anywhere else.
 raku -e 'for <AA NN> -> $alpha {  for (1..14) { say (state $ = $alpha)++;  
printf("d: %s\n", $ ) } }'
AA
Use of uninitialized value of type Any in string context.
Methods .^name, .raku, .gist, or .say can be used to stringify it to something 
meaningful.
  in block  at -e line 1
Use of uninitialized value of type Any in string context.
Methods .^name, .raku, .gist, or .say can be used to stringify it to something 
meaningful.
  in any join at gen/moar/stage2/NQPCORE.setting line 1075
d:
AB
Use of uninitialized value of type Any in string context.
...

break it out of the parens, and it loses some "stateness":
 raku -e 'for <AA NN> -> $alpha {  for (1..14) { say state $ = $alpha; $++;  
printf("d: %s\n", $ ) } }'
AA
Use of uninitialized value of type Any in string context.
Methods .^name, .raku, .gist, or .say can be used to stringify it to something 
meaningful.
  in block  at -e line 1
...
AA

but the named doesn't
 raku -e 'for <AA NN> -> $alpha {  for (1..14) { state $sv = $alpha; say $sv; 
$sv++;  printf("d: %s\n", $sv ) } }'
AA
d: AB
AB
d: AC




________________________________
From: William Michels <w...@caa.columbia.edu<mailto:w...@caa.columbia.edu>>
Sent: Tuesday, September 1, 2020 5:30 PM
To: Andy Bach <andy_b...@wiwb.uscourts.gov<mailto:andy_b...@wiwb.uscourts.gov>>
Cc: yary <not....@gmail.com<mailto:not....@gmail.com>>; perl6-users 
<perl6-us...@perl.org<mailto:perl6-us...@perl.org>>
Subject: Re: print particular lines question

My first clue that something is amiss is in your third line of code when the  
return skips "AA" and starts "AB, AC, AD....". That suggests to me that the two 
step assign/printf call is playing havoc with the $ anonymous variable. Try 
this instead:

~$ raku -e 'for <AA NN> -> $alpha {  for (1..14) { printf("d: %s\n", (state $ = 
$alpha)++ ) }; };'
d: AA
d: AB
d: AC
d: AD
d: AE
d: AF
d: AG
d: AH
d: AI
d: AJ
d: AK
d: AL
d: AM
d: AN
d: NN
d: NO
d: NP
d: NQ
d: NR
d: NS
d: NT
d: NU
d: NV
d: NW
d: NX
d: NY
d: NZ
d: OA

HTH, Bill.

On Tue, Sep 1, 2020 at 2:57 PM Andy Bach 
<andy_b...@wiwb.uscourts.gov<mailto:andy_b...@wiwb.uscourts.gov>> wrote:
I'm barely hanging on with the "$" so ... so from:
raku -e 'for <AA NN> -> $alpha { for (1..14) {   print (state $ = $alpha)++ ~ " 
"  } }'
AA AB AC AD AE AF

I tried an actual, er, non-anon var
# raku -e 'for <AA NN> -> $alpha { for (1..14) {   print (state $sv = $alpha)++ 
~ " "  } }'
AA AB AC AD AE AF ...

and then I tried
raku -e 'for <AA NN> -> $alpha {  for (1..14) { (state $sv = $alpha)++;  
printf("d: %s\n", $sv ) } }'
d: AB
d: AC
d: AD
d: AE
d: AF
...

but back to "$"
 raku -e 'for <AA NN> -> $alpha {  for (1..14) { (state $ = $alpha)++;  
printf("d: %s\n", $ ) } }'
Use of uninitialized value of type Any in string context.
Methods .^name, .raku, .gist, or .say can be used to stringify it to something 
meaningful.
  in block  at -e line 1
Use of uninitialized value of type Any in string context.
Methods .^name, .raku, .gist, or .say can be used to stringify it to something 
meaningful.
  in any join at gen/moar/stage2/NQPCORE.setting line 1075
d:

[27 more times]

I used printf hoping the %s context would stringify "$" as trying any of the 
suggested "methods" complain of a missing "self"
 raku -e 'for <AA NN> -> $alpha {  for (1..14) { (state $ = $alpha)++;  
printf("d: %s\n", $.raku ) } }'
===SORRY!=== Error while compiling -e
Variable $.raku used where no 'self' is available
at -e:1
------> v = $alpha)++;  printf("d: %s\n", $.raku⏏ ) } }
    expecting any of:
        term

So I'm missing something about "$", I think





________________________________
From: William Michels via perl6-users 
<perl6-us...@perl.org<mailto:perl6-us...@perl.org>>
Sent: Tuesday, September 1, 2020 3:17 PM
To: yary <not....@gmail.com<mailto:not....@gmail.com>>
Cc: perl6-users <perl6-us...@perl.org<mailto:perl6-us...@perl.org>>
Subject: Re: print particular lines question

I tried combining Larry's code and Yary's code, variously using
"state" or "INIT" or "BEGIN". This is what I saw:

~$ raku -e 'for <AA NN> -> $alpha { for (1..14) { print (state $ =
$alpha)++ ~ " " } }'
AA AB AC AD AE AF AG AH AI AJ AK AL AM AN NN NO NP NQ NR NS NT NU NV
NW NX NY NZ OA

~$ raku -e 'for <AA NN> -> $alpha { for (1..14) { print (INIT $ =
$alpha)++ ~ " " } }'
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27

~$ raku -e 'for <AA NN> -> $alpha { for (1..14) { print (BEGIN $ =
$alpha)++ ~ " " } }'
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27

Expected?  --Bill.

On Tue, Sep 1, 2020 at 11:44 AM yary 
<not....@gmail.com<mailto:not....@gmail.com>> wrote:
>
>
> Thanks, that's cool, and shows me something I was wondering about
>
> On Tue, Sep 1, 2020 at 11:36 AM Larry Wall 
> <la...@wall.org<mailto:la...@wall.org>> wrote:
>>
>> If you want to re-initialize a state variable, it's probably better to make
>> it explicit with the state declarator:
>>
>>     $ raku -e "for <a b> { for (1..2) { say (state $ = 'AAA')++ } }"
>>     AAA
>>     AAB
>>     AAA
>>     AAB
>
>
> $ raku -e 'for <AA OO> -> $alpha { for (1..3) { say (state $ = $alpha)++ } }'
> AA
> AB
> AC
> OO
> OP
> OQ
>

Reply via email to