raku

Why/how is an additional variable needed in matching repeated arbitary character with capture groups?

萝らか妹 提交于 2019-12-19 13:56:09
问题 I'm matching a sequence of a repeating arbitrary character, with a minimum length, using a perl6 regex. After reading through https://docs.perl6.org/language/regexes#Capture_numbers and tweaking the example given, I've come up with this code using an 'external variable': #uses an additional variable $c perl6 -e '$_="bbaaaaawer"; /((.){} :my $c=$0; ($c)**2..*)/ && print $0'; #Output: aaaaa To aid in illustrating my question only, a similar regex in perl5: #No additional variable needed perl -e

Parsing a possibly nested braced item using a grammar

纵饮孤独 提交于 2019-12-19 03:37:24
问题 I am starting to write BibTeX parser. The first thing I would like to do is to parse a braced item. A braced item could be an author field or a title for example. There might be nested braces within the field. The following code does not handle nested braces: use v6; my $str = q:to/END/; author={Belayneh, M. and Geiger, S. and Matth{\"{a}}i, S.K.}, END $str .= chomp; grammar ExtractBraced { rule TOP { 'author=' <braced-item> .* } rule braced-item { '{' <-[}]>* '}' } } ExtractBraced.parse(

Regex speed in Perl 6

牧云@^-^@ 提交于 2019-12-18 08:29:19
问题 I've been previously working only with bash regular expressions, grep , sed , awk etc. After trying Perl 6 regexes I've got an impression that they work slower than I would expect, but probably the reason is that I handle them incorrectly. I've made a simple test to compare similar operations in Perl 6 and in bash . Here is the Perl 6 code: my @array = "aaaaa" .. "fffff"; say +@array; # 7776 = 6 ** 5 my @search = <abcde cdeff fabcd>; my token search { @search } my @new_array = @array.grep({/

Does Baggy add (+) work on MixHash weights?

霸气de小男生 提交于 2019-12-18 07:44:34
问题 I am using a MixHash to combine two Hashes with the Bag add (+) operator. This seems to work - but ... I am a bit surprised that the result of the union needs to be re-coerced back to a MixHash. My guess is that the Bag add (+) infix operator coerces everything to a Bag first and returns the result as a Bag. This may be risky for me as some of my weights are negative (thus the Mix in the first place). Will this properly add negative weights? Alternatively, is there a Mix add (+) operator? my

Does Baggy add (+) work on MixHash weights?

亡梦爱人 提交于 2019-12-18 07:42:15
问题 I am using a MixHash to combine two Hashes with the Bag add (+) operator. This seems to work - but ... I am a bit surprised that the result of the union needs to be re-coerced back to a MixHash. My guess is that the Bag add (+) infix operator coerces everything to a Bag first and returns the result as a Bag. This may be risky for me as some of my weights are negative (thus the Mix in the first place). Will this properly add negative weights? Alternatively, is there a Mix add (+) operator? my

How do I “flatten” a list of lists in perl 6?

孤人 提交于 2019-12-18 03:24:08
问题 Let's say I want all permutations of 2 letters out of a, b and c. I can do: my @perm = <a b c>.combinations(2)».permutations; say @perm; # [((a b) (b a)) ((a c) (c a)) ((b c) (c b))] which is close, but not exactly what I need. How do I “flatten” this so that I get: # [(a b) (b a) (a c) (c a) (b c) (c b)] ? 回答1: See also "a better way to accomplish what I (OP) wanted". See also "Some possible solutions" answer to "How can I completely flatten a Perl 6 list (of lists (of lists) … )" question.

How do I match a hex array in perl6 grammar

大城市里の小女人 提交于 2019-12-13 12:55:44
问题 I have a string like "39 3A 3B 9:;" and i want to extract "39, 3A, 3B" I have tried my $a = "39 3A 3B 9:;"; grammar Hex { token TOP { <hex_array>+ .* } token hex_array { <[0..9 A..F]> " " } }; Hex.parse($a); But this doesn't seem to work. And even this doesn't seem to work. my $a = "39 3A 3B "; grammar Hex { token TOP { <hex_array>+ } token hex_array { <[0..9 A..F]> " " } }; Hex.parse($a); I did try Grammar::Tracer both TOP and hex_array fail TOP | hex_array | * FAIL * FAIL 回答1: <[abcdef...]>

Which context confuses this Perl 6 zip operator?

假装没事ソ 提交于 2019-12-13 12:26:51
问题 Consider this program where I create a hash. I want to then change two values in it: my $hash = %( wallet => 100, gave => 0, received => 0, ); for ^1 { $hash<wallet gave> Z+= <-1 1> }; dd $hash; Like this, the last line of for doesn't do anything and there is no warning. The hash is unchanged: Hash $hash = ${:gave(0), :received(0), :wallet(100)} Adding another statement changes the behavior: my $hash = %( wallet => 100, gave => 0, received => 0, ); for ^1 { $hash<wallet gave> Z+= <-1 1>; True

how to load Perl5's Data::Printer in Perl6?

大城市里の小女人 提交于 2019-12-13 12:26:50
问题 I've been trying to load in the Perl5 module Data::Printer into Perl6, but am having a hard time. I asked this earlier, Cannot import Perl5 module using Inline::Perl5 into Perl6 and did get useful advice from @raiph and Elizabeth, but was advised to do another question con@con-VirtualBox:~$ perldoc -lm Data::Printer /usr/local/share/perl/5.26.0/Data/Printer.pm con@con-VirtualBox:~$ perl6 To exit type 'exit' or '^D' > use Inline::Perl5; Nil > use lib:from<Perl5> '/usr/local/share/perl/5.26.0

Perl6: check if STDIN has data

前提是你 提交于 2019-12-13 12:08:41
问题 In my Perl 6 script, I want to do a (preferably non-blocking) check of standard input to see if data is available. If this is the case, then I want to process it, otherwise I want to do other stuff. Example ( consumer.p6 ): #!/usr/bin/perl6 use v6.b; use fatal; sub MAIN() returns UInt:D { while !$*IN.eof { if some_fancy_check_for_STDIN() { #TODO: this needs to be done. for $*IN.lines -> $line { say "Process '$line'"; } } say "Do something Else."; } say "I'm done."; return 0; } As a STDIN