Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
259 views
in Technique[技术] by (71.8m points)

I want to split a string into array from 2nd element using Perl

I have a string "sample.1.csv.main" want to split into array from 2nd element.

Here is the code I had tried

Code --

my $str = "sample.1.csv.main";
my @arra = split('.',$str,2);

print "first element : $arr[0]";

OUTPUT :

first element : sample

DESIRE OUTPUT :

first element : sample.1

question from:https://stackoverflow.com/questions/65916743/i-want-to-split-a-string-into-array-from-2nd-element-using-perl

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Split on dots, then join the first two elements:

my $str = 'sample.1.csv.main';
my @arra = split /./, $str;
splice @arra, 0, 2, join '.', @arra[0, 1];

See join and splice.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...