如何在Perl中连接两个子字符串 [英] How to join two substrings in perl
问题描述
如何仅联接字符串数组的那些包含其位置由用户通过命令行输入位置的字符的子字符串?
How to join only those substrings of a string array which contain a character whose positions were entered by user through command line?
例如:如果$string
是"MALWCMRALPLCLALALCAWGPDPACAFVN"
并且@array
包含$string
中'A'
处每个剪切的子字符串.那么如何将两个包含在原始字符串中3和9位置(由用户通过命令行输入)的'L'
的子字符串连接在一起?
For example: if $string
is "MALWCMRALPLCLALALCAWGPDPACAFVN"
and @array
contains substrings of every cut at 'A'
in $string
. Then how could two substrings which contains 'L'
at positions 3 and 9 (entered by user through command line) in the original string be joined?
推荐答案
我不确定您确切打算做什么,但是要使用连接运算符将两个字符串连接起来
I'm not sure what you precisely intend to do, but to join two strings you use the concatenation operator
$string3 = $string1 . $string2 ;
要加入字符串数组,您可以
to join an array of strings, you can do
$string3 = join( "", @stringarr ) ;
您可以使用grep
在数组中搜索包含L
的字符串:
You can search the array for strings containing L
using grep
:
@selstrings = grep( /L/, @strarray ) ;
并加入它:
$string3 = join( "", @selstrings ) ;
使用grep中的模式,您可以定义L的位置:
Using the pattern in grep, you can define at which positions you want the L's to be:
@selstrings = grep( /^(.{2}|.{8})L/, @starray ) ;
自然地,您可以首先使用命令行中的变量来构造正则表达式:
Naturally, you can construct the regex using variables from command line first:
$regex = "^(.{" . $ARGV[0] . "}|.{" . $ARGV[1] . "})L" ;
@selstrings = grep( /$regex/, @starray ) ;
这篇关于如何在Perl中连接两个子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!