问题描述
我有一个这样的字符串,你好吗
,我想得到像
I have a string like this , how are you
, I want to get all possible shuffle of word like
how are you
are how you
you how are
you are how
are you how
how you are
如何在 perl
脚本中创建它,我已经尝试了 shuffle
函数,但它只返回一串 shuffle .
如果你不熟悉 Perl
脚本,你可以只告诉我逻辑.
How can I make it in perl
script , i've tried the shuffle
function but it returns only one string of shuffle .
If you are not familiar with Perl
script, you can tell me the logic only.
注意:字符串中的字数不是恒定的.
Note: The words count in string are not constant.
推荐答案
你说的是 permutations.这可以在 Perl 中使用 Algorithm::Permute
模块完成:
What you're talking about are permutations. This can be done in Perl with the Algorithm::Permute
module:
如果您已经安装了该模块,这里有一个 shell one-liner 可以为您完成:
If you've installed the module, here's a shell one-liner that will do it for you:
perl -e'
use Algorithm::Permute qw();
my $str = $ARGV[0];
my @arr = split(/\s+/,$str);
my $ap = new Algorithm::Permute(\@arr);
while (my @res = $ap->next()) { print("@res\n"); }
' 'how are you';
## you are how
## are you how
## are how you
## you how are
## how you are
## how are you
这篇关于使用 perl 脚本获取字符串中单词的所有可能排列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!