Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does split not return anything?

Tags:

split

perl

I am trying to get that Perl split working for more than 2 hours. I don't see an error. Maybe some other eyes can look at it and see the issue. I am sure its a silly one:

@versionsplit=split('.',"15.0.3");

print $versionsplit[0];
print $versionsplit[1];
print $versionsplit[2];

I just get an empty array. Any idea why?

like image 842
dom Avatar asked Apr 22 '26 05:04

dom


2 Answers

You need:

@versionsplit=split(/\./,"15.0.3");

The first argument to split is a regular expression, not a string. And . is the regex symbol which means ‘match any character’. So all the characters in your input string were being treated as separators, and split wasn't finding anything between them to return.

like image 101
Smylers Avatar answered Apr 24 '26 17:04

Smylers


the "." represents any character.You need to escape it for split function to recognise as a field separator. change your line to

@versionsplit=split('\.',"15.0.3");
like image 38
Vijay Avatar answered Apr 24 '26 17:04

Vijay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!