HTTP 请求的响应是一个类似这样的字符串
'one=iwejdoewde&two=ijijjiiij&three=&four=endinghere'
我想把这个查询放入哈希表中。我的解决方案是
my $s = 'one=iwejdoewde&two=ijijjiiij&three=&four=endinghere';
my %h = $s.comb(/<-[&]>+/).map({ my @a = .split(/\=/); @a[0] => @a[1] }) ;
say %h;
#
# {four => endinghere, one => iwejdoewde, three => , two => ijijjiiij}
#
我觉得这个$s
看起来%h
很丑。不过,它处理three=
片段时不会中断。
似乎应该有更好的方法,特别是根据的结果来配对split
。
这似乎对单个对有用:
my $s='one=two';
say $s.match( / (.+) \= (.*) / ).pairup
但把它放进去.map
会导致意想不到的结果
my $s = 'one=iwejdoewde&two=ijijjiiij&three=&four=endinghere';
my %h = $s.comb(/<-[&]>+/).map( *.match(/ (.+) \= (.*) /).pairup )
say %h;
#
# {one iwejdoewde => (「two」 => 「ijijjiiij」), three => (「four」 => 「endinghere」)}
#