rotor in comb?
hello people, Here is a little raku code to count all the digraphs from some text: my %digraphs = slurp.lc .comb(/<[a..z]>+/) .map( *.comb.rotor( 2 => -1, :!partial ).map: *.join ) .Bag; The first .comb match words then I map them to get digraphs but I think it is possible to move the cursor backward in the comb regex. I just don't know how. I'm trying to write something like: slurp.lc .comb(/ <[a..z]> ** 2 { --pos } /) .Bag; The perl version of it is: perl -lnE ' END { say "$_ = $digraph{$_}" for sort { $digraph{$b} <=> $digraph{$a} } keys %digraph } $_=lc; while (/([a-z]{2})/g) {++$digraph{$1}; --pos; } ' Any help is very welcome. regards, -- Marc Chantreux Pôle de Calcul et Services Avancés à la Recherche (CESAR) http://annuaire.unistra.fr/p/20200
Re: rotor in comb?
> On Aug 27, 2022, at 10:56 AM, Marc Chantreux wrote: --snip-- > but I think it is possible to move the cursor backward in the comb regex. --snip-- I do *not* think you can ("move the cursor backward in the comb regex"); See https://docs.raku.org/routine/comb : ... "returns a Seq of non-overlapping matches" ... The "non-overlapping" nature is the problem. (Please let me know if this turns out to be incorrect!) In foresight, Raku has added an optional `:exhaustive` flag to regex matching, and that will do what you want. This Raku code: my %digraphs = slurp.lc.match(:exhaustive, /(<[a..z]> ** 2)/)».Str.Bag; .say for %digraphs.sort({ -.value, ~.key }); , produces output identical to this Perl code: perl -lnE ' END { say "$_ => $digraph{$_}" for sort { $digraph{$b} <=> $digraph{$a} || $a cmp $b } keys %digraph } $_=lc; while (/([a-z]{2})/g) {++$digraph{$1}; --pos; } ' Camelia.svg , when run against a downloaded copy of our mascot: https://upload.wikimedia.org/wikipedia/commons/8/85/Camelia.svg -- Hope this helps, Bruce Gray (Util of PerlMonks)
Re: rotor in comb?
Hi Marc (and Bruce)! I'm adapting a "word frequency" answer posted by Sean McAfee on this list. The key seems to be adding the `:exhaustive` adverb to the `match` call. AFAIK comb will not accept this adverb, so `match will have to do for now: Sample Input (including quotes): “A horse, a horse, my kingdom for a horse!” ~$ raku -e '++(my %digraphs){$_} for slurp.lc.match(:global, :exhaustive, /<[a..z]>**2/); .say for %digraphs.sort(-*.value);' Sample Output: or1 => 4 se1 => 3 rs1 => 3 ho1 => 3 in1 => 1 my1 => 1 om1 => 1 ki1 => 1 ng1 => 1 do1 => 1 gd1 => 1 fo1 => 1 HTH, Bill. On Sat, Aug 27, 2022 at 10:25 AM Bruce Gray wrote: > > > > On Aug 27, 2022, at 10:56 AM, Marc Chantreux wrote: > > --snip-- > > > but I think it is possible to move the cursor backward in the comb regex. > > --snip-- > > I do *not* think you can ("move the cursor backward in the comb regex"); > See https://docs.raku.org/routine/comb : > ... "returns a Seq of non-overlapping matches" ... > The "non-overlapping" nature is the problem. > (Please let me know if this turns out to be incorrect!) > > In foresight, Raku has added an optional `:exhaustive` flag to regex > matching, and that will do what you want. > This Raku code: > > my %digraphs = slurp.lc.match(:exhaustive, /(<[a..z]> ** > 2)/)».Str.Bag; > .say for %digraphs.sort({ -.value, ~.key }); > > , produces output identical to this Perl code: > > perl -lnE ' > END { say "$_ => $digraph{$_}" for > sort { $digraph{$b} <=> $digraph{$a} || $a cmp $b } > keys %digraph > } > $_=lc; while (/([a-z]{2})/g) {++$digraph{$1}; --pos; } > ' Camelia.svg > > , when run against a downloaded copy of our mascot: > https://upload.wikimedia.org/wikipedia/commons/8/85/Camelia.svg > > -- > Hope this helps, > Bruce Gray (Util of PerlMonks) > >
Re: rotor in comb?
Heads-up: code was correct in my last post, but the output is as follows (Rakudo v2021.06): ~$ raku -e '++(my %digraphs){$_} for slurp.lc.match(:global, :exhaustive, /<[a..z]>**2/); .say for %digraphs.sort(-*.value);' richard3.txt or => 4 rs => 3 ho => 3 se => 3 gd => 1 in => 1 fo => 1 om => 1 do => 1 ng => 1 ki => 1 my => 1 On Sat, Aug 27, 2022 at 10:45 AM William Michels wrote: > Hi Marc (and Bruce)! > > I'm adapting a "word frequency" answer posted by Sean McAfee on this list. > The key seems to be adding the `:exhaustive` adverb to the `match` call. > AFAIK comb will not accept this adverb, so `match will have to do for now: > > Sample Input (including quotes): “A horse, a horse, my kingdom for a > horse!” > > ~$ raku -e '++(my %digraphs){$_} for slurp.lc.match(:global, :exhaustive, > /<[a..z]>**2/); .say for %digraphs.sort(-*.value);' > > Sample Output: > > or1 => 4 > se1 => 3 > rs1 => 3 > ho1 => 3 > in1 => 1 > my1 => 1 > om1 => 1 > ki1 => 1 > ng1 => 1 > do1 => 1 > gd1 => 1 > fo1 => 1 > > HTH, Bill. > > > On Sat, Aug 27, 2022 at 10:25 AM Bruce Gray wrote: > >> >> >> > On Aug 27, 2022, at 10:56 AM, Marc Chantreux wrote: >> >> --snip-- >> >> > but I think it is possible to move the cursor backward in the comb >> regex. >> >> --snip-- >> >> I do *not* think you can ("move the cursor backward in the comb regex"); >> See https://docs.raku.org/routine/comb : >> ... "returns a Seq of non-overlapping matches" ... >> The "non-overlapping" nature is the problem. >> (Please let me know if this turns out to be incorrect!) >> >> In foresight, Raku has added an optional `:exhaustive` flag to regex >> matching, and that will do what you want. >> This Raku code: >> >> my %digraphs = slurp.lc.match(:exhaustive, /(<[a..z]> ** >> 2)/)».Str.Bag; >> .say for %digraphs.sort({ -.value, ~.key }); >> >> , produces output identical to this Perl code: >> >> perl -lnE ' >> END { say "$_ => $digraph{$_}" for >> sort { $digraph{$b} <=> $digraph{$a} || $a cmp $b } >> keys %digraph >> } >> $_=lc; while (/([a-z]{2})/g) {++$digraph{$1}; --pos; } >> ' Camelia.svg >> >> , when run against a downloaded copy of our mascot: >> https://upload.wikimedia.org/wikipedia/commons/8/85/Camelia.svg >> >> -- >> Hope this helps, >> Bruce Gray (Util of PerlMonks) >> >>
Re: Inconsistencies with "with" chained to "for"
Looks like it worth a bug report. I was probably stumbling upon this too for a couple of times. Best regards, Vadim Belman > On Aug 27, 2022, at 2:24 AM, Fernando Santagata > wrote: > > Hello, > I noticed this behavior: > > [0] > my @a = > [a b c d e] > [1] > .say with $_ for @a > () > [2] > .say if .defined for @a > a > b > c > d > e > [3] > (.say with $_) for @a > a > b > c > d > e > [4] > (.say if .defined) for @a > a > b > c > d > e > > Apparently in this case "with" works only as a statement modifier while "if" > works both ways. > Is this a known behavior with well understood reasons, or should I open an > issue? > > -- > Fernando Santagata