Split Function in an array

What is split Function?
How to delimit a string using perl?

Explanation

Split function:


Split() is used to split or delimit a string to make an array, by using the specified delimiters.

Syntax:


split( delimiter, string);

In the above syntax "delimiter" is the delimit operator or symbol to be used, "string" specifies the string to split.

Example :


#! C:programfilesperlbinperl
print "content-type: text/htmlnn";
$fruits="grapes+cherry+orange";
@f=split(/+/,$fruits);
print "@f";
Result :

grapes cherry orange

In the above example, the split function delimit the + symbol and displays an array element. The string has "+" as the delimiter, to know that its not an operator but a delimiter we use the backslash in between forward slashes. Now the string is split into an 3 element array.

Example :


#! C:programfilesperlbinperl
print "content-type: text/htmlnn";
$fruits="grapes:cherry:orange";
@f=split(/:/,$fruits);
print "@f";
Result :

grapes cherry orange

In the above example, we used ":" as the seperator to split string, here we use only two forward slashes, not the backward slash in between as ":" is not an operator like "+" symbol.

Ask Questions

Ask Question