stackoverflow0208

parse string with pairs of values into hash the Perl6 way

解析下面的文本, 将其转换为散列:

width=13
height=15
name=Mirek

Answer1

my $text = "width=13\nheight=15\nname=Mirek";
$text ~~ / [(\w+) \= (\N+)]+ %% \n+ /;
my %params = $0>>.Str Z=> $1>>.Str;

Answer2

my %params = $text.comb(/\w+\=\N+/)>>.split("=").flat;

Answer3

my %params = $text.split("\n")>>.split("=").flat;

Answer4

my %params = $text.lines>>.split("=").flat;

Sorting hash kv pairs

http://stackoverflow.com/questions/39307797/can-i-return-multiple-pairs-from-a-map-feeding-into-a-hash?rq=1

怎么得到散列的元素个数?
How to get the count of no. of keys in a perl 6 %HASH?

%hash.elems 
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容