|
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:\programfiles\perl\bin\perl
print "content-type: text/html\n\n";
$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:\programfiles\perl\bin\perl
print "content-type: text/html\n\n";
$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.
|