Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split by dot using Perl

Tags:

split

perl

I use the split function by two ways. First way (string argument to split):

my $string = "chr1.txt";
my @array1 = split(".", $string);
print $array1[0];

I get this error:

Use of uninitialized value in print

When I do split by the second way (regular expression argument to split), I don't get any errors.

my @array1 = split(/\./, $string); print $array1[0];

My first way of splitting is not working only for dot.

What is the reason behind this?

like image 566
I am Avatar asked Aug 25 '12 16:08

I am


2 Answers

"\." is just ., careful with escape sequences.

If you want a backslash and a dot in a double-quoted string, you need "\\.". Or use single quotes: '\.'

like image 61
Mat Avatar answered Sep 20 '22 15:09

Mat


If you just want to parse files and get their suffixes, better use the fileparse() method from File::Basename.

like image 36
snoofkin Avatar answered Sep 20 '22 15:09

snoofkin