Softpanorama

Home Switchboard Unix Administration Red Hat TCP/IP Networks Neoliberalism Toxic Managers
May the source be with you, but remember the KISS principle ;-)
Skepticism and critical thinking is not panacea, but can help to understand the world better

Perl Split() Function and option g in matching

News

Perl Regular Expressions

Recommended Links Best Books Perl control structures grep & map
Perl as a command line utility tool Debugging Perl IDE Perl power tools Reimplementation of Unix tools Extended Notation and Commenting Regular Expressions
Greedy and Non-Greedy Matches Perl Split function Perl HTML Matching Examples Regular Expressions Best Practices index and rindex in Perl Perl tr function

Beautifiers

Perl Warts

 Perl philosophy and history Tips Perl regex history Etc

Split function is one the few Perl functions that have regular expression as an argument. Its purpose is to take a string and convert it to an array or list breaking at points where the first argument (delimiter) specified with the regular expression matches. 

The usual syntax for the split function is

list = split (pattern, string_value);

Here, string_value is the string to be split. pattern  is a regular expression to be searched for. Again, it is important to understand that a new element is started every time pattern is matched; pattern  itself is not included as part of any element serving as a separator between elements.).  The resulting list of elements is returned in list.

For example, the following statement breaks the character string stored in $line into elements delimited by ":", and store them into the array @tokens:

@tokens = split (/:/, $line);

You can specify the maximum number of elements of the list produced by split by specifying the maximum as the third argument. For example:

$line = "This:is:a:string";

@tokens = split (/:/, $line, 3);

As before, this breaks the string stored in $line into elements. After two first elements have been created, no more new elements are created. The rest of the string is assigned to the third element of arrays. A In this case, the list assigned to @list is ("This", "is", "a:string").

You can also assign to several scalar variables at once:

$line = "11 12 13 14 15";
($var1, $var2, $line) = split (/\s+/, $line, 3);
This splits $line into the list ("11", "12", "13 14 15"). $var1 is assigned 11, $var2 is assigned 12, and $line is assigned "13 14 15". This enables you to assign the "leftovers" to a single variable, which can then be split again at a later time

Additional examples

$_ = 'AB AB AC';
print m/c$/i



Etc

The Last but not Least Technology is dominated by two types of people: those who understand what they do not manage and those who manage what they do not understand ~Archibald Putt. Ph.D


Copyright 1996-2018 by Dr. Nikolai Bezroukov. www.softpanorama.org was initially created as a service to the (now defunct) UN Sustainable Development Networking Programme (SDNP) in the author free time and without any remuneration. This document is an industrial compilation designed and created exclusively for educational use and is distributed under the Softpanorama Content License. Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.

FAIR USE NOTICE This site contains copyrighted material the use of which has not always been specifically authorized by the copyright owner. We are making such material available to advance understanding of computer science, IT technology, economic, scientific, and social issues. We believe this constitutes a 'fair use' of any such copyrighted material as provided by section 107 of the US Copyright Law according to which such material can be distributed without profit exclusively for research and educational purposes.

This is a Spartan WHYFF (We Help You For Free) site written by people for whom English is not a native language. Grammar and spelling errors should be expected. The site contain some broken links as it develops like a living tree...

You can use PayPal to make a contribution, supporting development of this site and speed up access. In case softpanorama.org is down you can use the at softpanorama.info

Disclaimer:

The statements, views and opinions presented on this web page are those of the author (or referenced source) and are not endorsed by, nor do they necessarily reflect, the opinions of the author present and former employers, SDNP or any other organization the author may be associated with. We do not warrant the correctness of the information provided or its fitness for any purpose.

The site uses AdSense so you need to be aware of Google privacy policy. You you do not want to be tracked by Google please disable Javascript for this site. This site is perfectly usable without Javascript.

Last modified: March 12, 2019