1

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

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

I think no issues while doing like this:

my $str = "sample.1.csv.main";

my @arra = split('\.',$str);

print "first element : $arra[0].$arra[1]";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.