我编写了这段代码来查找每个x元素的子字符串:
print "enter file path\n";
$letters = <>;
chomp ($letters);
$sequence = "";
open (LETTERS, $letters) or die "error opening\n";
print "how many letters at a shot\n";
$number = <>;
chomp ($number);
$size = length $sequence;
chomp ($size);
for ($i = 0; $i < $size; $i++) {
$test = substr ($sequence, $i, $number);
print "> Test $i\n";
print "$test\n";
if ($i >= $size - $number) {
last;
}
}
所以如果我用这个字符串打开一个文件并选择x = 3:
abcdefg
我得到了这个结果:
> Test 0 abc
> Test 1 bcd
> Test 2 cde
> Test 3 def
> Test 4 efg
每个子字符串与前一个子字符串的位置不同,我希望能够控制这个数字并将子字符串移位2。结果将是:
> Test 0 abc
> Test 2 cde
> Test 4 efg
有什么建议吗?感谢
答案 0 :(得分:0)
添加:
...
$step = <>;
...
并改变:
for ($i = 0; $i < $size; $i++) {
到
for ($i = 0; $i < $size; $i+=$step) {
答案 1 :(得分:0)
for ($i = 0; $i < $size; $i += 2) {