Perl 如何在用户指定的位置切换两个底座

Perl 如何在用户指定的位置切换两个底座,perl,bioinformatics,Perl,Bioinformatics,我是一名PERL初学者,正在研究上述问题。所以我犯了这个错误 拼接()偏移量超过数组的末尾,使用后使用strict 我花了几个小时修改代码,但没有用,所以请任何人向我解释为什么它不能像外行一样工作(我完全是个新手) 谢谢 #!usr/bin/perl use strict; use warnings; #Ask for input from user #Then switch two bases at positions specified by the user print "Enter

我是一名PERL初学者,正在研究上述问题。所以我犯了这个错误 拼接()偏移量超过数组的末尾,使用后使用strict

我花了几个小时修改代码,但没有用,所以请任何人向我解释为什么它不能像外行一样工作(我完全是个新手)

谢谢

#!usr/bin/perl

use strict;
use warnings;

#Ask for input from user
#Then switch two bases at positions specified by the user

print "Enter your DNA string:\n";
my @input_seq = split( //, <STDIN> );
chomp @input_seq;
print "First base: ";    #position of first base
my $base_1_pos = <STDIN>;
chomp $base_1_pos;
my $base_1 = "$input_seq[$base_1_pos]";
print "Second base ";    #position of second base
my $base_2_pos = <STDIN>;
chomp $base_2_pos;
my $base_2 = "$input_seq[$base_2_pos]";
@input_seq = splice( @input_seq, "$base_1_pos", 1, "$base_2" );    #splice $base_2 into $base_1
@input_seq = splice( @input_seq, "$base_2_pos", 1, "$base_1" );    #splice $base_1 into $base_2
print "@input_seq\n\n";                                            #print output
#!usr/bin/perl
严格使用;
使用警告;
#请求用户输入
#然后在用户指定的位置切换两个底座
打印“输入您的DNA字符串:\n”;
my@input_seq=拆分(//,);
chomp@input_seq;
打印“第一基础:”#一垒位置
我的$base_1_pos=;
chomp$base_1_pos;
my$base_1=“$input_seq[$base_1_pos]”;
打印“第二垒”#二垒位置
我的$base_2_pos=;
chomp$base_2_pos;
my$base_2=“$input_seq[$base_2_pos]”;
@输入顺序=拼接(@input顺序,$base\U 1\U pos,$base\U 2”)#将$base_2拼接到$base_1中
@输入顺序=拼接(@input顺序,$base_2_pos,$base_1”)#将$base_1拼接到$base_2中
打印“@input_seq\n\n”#打印输出
致以最良好的祝愿,

只需更改这两行:

@input_seq= splice (@input_seq, "$base_1_pos", 1, "$base_2");   #splice $base_2 into $base_1
@input_seq= splice (@input_seq, "$base_2_pos", 1, "$base_1");   #splice $base_1 into $base_2
致:

正如报告中所说:

拼接阵列或EXPR、偏移、长度、列表
从数组中删除由偏移量和长度指定的元素, 并将其替换为列表中的元素(如果有)。在列表上下文中, 返回从数组中删除的元素。在标量上下文中, 返回最后删除的元素,如果未删除任何元素,则返回undef


如果您只想交换数组中的两个元素,则根本不需要
拼接
。An可以更轻松、更高效地完成这项工作:

@input_seq[$base_1_pos, $base_2_pos] = @input_seq[$base_2_pos, $base_1_pos];
或者,如果已将元素值保存在标量变量中:

@input_seq[$base_1_pos, $base_2_pos] = ($base_2, $base_1);
甚至简单地说:

$input_seq[$base_1_pos] = $base_2;
$input_seq[$base_2_pos] = $base_1;

(你需要<代码>拼接< /代码>的时候,你要用一个不同长度的序列替换数组中间的元素序列,但是如果你只想替换一些元素而不改变数组的长度,那么切片分配就行了。)

您能否提供示例输入来说明问题?
$input_seq[$base_1_pos] = $base_2;
$input_seq[$base_2_pos] = $base_1;