一个基本的例子是我有一个数组,['abc','cde','efg']并希望将它分成两个数组.一个元素包含a c和一个元素.
在红宝石中,我只想说:
has_c, no_c = arr.partition { |a| a.include?('c') }
有一个简单的perl等价物吗?
有这样的part功能List::MoreUtils:
use List::MoreUtils 'part';
my $listref = [ 'abc', 'cde', 'efg' ];
my ($without_c, $with_c) = part { /c/ } @$listref;
print "with c : @$with_c\n";
print "without: @$without_c\n";
输出:
with c : abc cde
without: efg