Perl shuffle字母

时间:2015-03-23 18:39:41

标签: perl

完成下面我有字符串,它被每个字符分开。我想拆分每个有4个或更多字母的单词。基本上$ string [0]和[-1]保持不变,其间的其他元素应该随机改组。 然后加入元素0和-1。这对我来说非常困难。 有什么想法吗?

我想要的输出会是这样的(每次都不同): 我很高兴能够成为一名职员。

 #!/usr/bin/perl
        use List::Util qw(shuffle);

        my $string= "I have constructed a string that needs to be shuffled";

        my @characters = split(//,$string);
        foreach (@characters) {
        print($_, "\n");
        }

@characters = shuffle($character[1](@characters[1..$characters-1]));

4 个答案:

答案 0 :(得分:1)

你走在正确的轨道上。

#!/usr/bin/perl
use strict;
use warnings;
use List::Util qw(shuffle);

my $string= "I have constructed a string that needs to be shuffled";

my @characters = split(//,$string);

foreach (@characters) {
    print($_, "\n");
}

my @shuffled;

# assign the first and last elements to the shuffled array
$shuffled[0] = $characters[0];
$shuffled[$#characters] = $characters[$#characters];

@shuffled[1 .. $#characters - 1]  = shuffle(@characters[1 .. $#characters - 1]);

foreach (@shuffled) {
    print($_, "\n");
}

的问题:

  1. 使用严格,使用警告。您的代码是指您没有定义的@character数组:$character[1]
  2. $#array给出@array的最后一个索引(不是长度!)。您可以在标量上下文($ array)中使用该数组,该数组给出了您想要最后一个索引的长度。
  3. 更正了列表切片语法

答案 1 :(得分:1)

如果我理解你想要的东西,你就有这样的字符串:

 I have constructed a string that needs to be shuffled

你想要洗牌所有的话。但是,如果单词是一个小单词,则需要将其与较长的单词组合。例如,您想要像这样划分字符串:

 I | have | constructed | a string | that | needs | to be | shuffled

您希望第一个和最后一个字保持不变:

 I ??? shuffled

你希望所有其他人都洗牌了:

 I constructed have needs that a string to be | shuffled

我喜欢一次拿东西:

  1. 将字符串分成单独的字词。
  2. 创建一个包含您想要随机播放的所有单词的数组。
  3. 浏览一系列单词,如果单词小于最小金额,请附加以下单词。
  4. 随机播放您在步骤3中创建的阵列
  5. 打印你的句子
  6. 这是程序:

    #! /usr/bin/env perl
    
    use strict;
    use warnings;
    use feature qw(say);
    
    use List::Util qw(shuffle);
    
    use constant {
        MIN_WORD_LENGTH => 4,
        SENTENCE        => "I have constructed a string that needs to be shuffled",
    };
    
    
    #
    # Split the sentence into words
    #
    my @sentence = split /\s+/, SENTENCE;
    
    #
    # @words are the words to be shuffled
    #
    my @words = @sentence[1..$#sentence - 1];
    
    my @combined_words;     #Same as @words but smaller words are combined
    my $word_to_push;
    for my $word (@words) {
        #
        # Combine short words with the next one
        #
        if ( not defined $word_to_push ) {
            $word_to_push = $word;
        }
        else {
            $word_to_push .= " " . $word;
        }
        #
        # If the word_to_push is long enough, push it
        #
        if ( length $word_to_push >= MIN_WORD_LENGTH ) {
            push @combined_words, $word_to_push;
            undef $word_to_push;
        }
    }
    
    #
    # Now shuffle the words
    my @shuffle_words  = shuffle @combined_words;
    say "$sentence[0] @shuffle_words $sentence[-1]";
    

    结果是(运行三次):

    I that a string have constructed needs to be shuffled
    I to be that a string needs constructed have shuffled
    I that to be a string constructed have needs shuffled
    

答案 2 :(得分:0)

这就是你要找的东西。我无法想出我应该提供什么评论,所以如果您需要,请提出问题

use strict;
use warnings;

use List::Util qw(shuffle);

my $string= "I have constructed a string that needs to be shuffled";
my @ch = split //, $string;
my $shuffled = join '', $ch[0], shuffle(@ch[1..$#ch-1]), $ch[-1];

print $shuffled, "\n";

示例输出

Isedr  hricfes  enandts afltashnou t gothetucte evb d

<强>更新

我想我更了解你,虽然你还没有确认。这可能就是你要求的。但这是一个功课问题吗?

use strict;
use warnings;

use List::Util qw/ shuffle /;

my $string= "I have constructed a string that needs to be shuffled";

my @string = map {
  /\S/ && length($_) > 4 ? shuffle_word($_) : $_
} split /(\s+)/, $string;

print join('', @string), "\n";


sub shuffle_word {
  my ($word) = @_;
  my @word = split //, $word;
  join '', $word[0], shuffle(@word[1..$#word-1]), $word[-1];
}

示例输出

I have csrtuoetcnd a snrtig that ndees to be shlfeufd

答案 3 :(得分:-1)

如果你想用四个或更多个字符随机播放每个单词,你可以这样做。

  • my @ch = split //, shiftshift所采用的函数参数(它是shift(@_)的简写)拆分为每个字符。

  • # /只是为了解决错误的语法突出显示问题。

  • join '', $ch[0], shuffle( @ch[ 1 .. $#ch - 1 ] ), $ch[-1]随机播放并将字符串连接在一起。

use strict;
use warnings;
use List::Util qw(shuffle);

my $string = "I have constructed a string that needs to be shuffled";

sub shuffle_str {
    my @ch = split //, shift; # /
    join '', $ch[0], shuffle( @ch[ 1 .. $#ch - 1 ] ), $ch[-1];
}

my $shuffled = $string =~ s/(\w{4,})/shuffle_str($1)/ger;

print $shuffled, "\n";

示例输出

I have cutscntroed a sritng taht nedes to be sufehfld