Perl split Function



Description

This function splits a string expression into fields based on the delimiter specified by PATTERN. If no pattern is specified whitespace is the default. An optional limit restricts the number of elements returned.

A negative limit has the same effect as no limit. This function is often used in conjunction with join() to create small text databases.

Syntax

Following is the simple syntax for this function −

split /PATTERN/, EXPR, LIMIT

split /PATTERN/, EXPR

split /PATTERN/

split

Return Value

  • Return Value in Scalar Context: Not recommended, but it returns the number of fields found and stored the fields in the @_ array.

  • Return Value in Array Context: A list of fields found in EXPR or $_ if no expression is specified.

Example

Following is the example code showing its basic usage −

#!/usr/bin/perl -w

@fields = split(/:/, "1:2:3:4:5");
print "Field values are: @fields\n";

When above code is executed, it produces the following result −

Field values are: 1 2 3 4 5
perl_function_references.htm
Advertisements