split and join

split

split cuts a string wherever the pattern matches and returns the pieces as a list. The pattern describes the separator, not the fields, which is the one thing to remember about it.

my $line = "ada:36:london"; my @fields = split /:/, $line; print "@fields\n"; # ada 36 london print scalar @fields, "\n"; # 3

perl demo.pl

ada 36 london
3

A plain string may be used instead of a pattern for a fixed separator, but the pattern form keeps the habit consistent and lets you add alternatives later, such as /[:,]/.

The third argument

By default split drops trailing empty fields, which is convenient for loose input and wrong for fixed columns. Give a limit as the third argument to keep them, or use a negative limit to keep every empty field.

my $csv = "a,b,,"; my @loose = split /,/, $csv; # a b my @fixed = split /,/, $csv, -1; # a b and two empty fields print scalar @fixed, "\n"; # 4

perl demo.pl

4

A positive limit caps the number of fields, and the remainder stays in the last one. That is how you split a line into key and value only once.

split on whitespace

The special pattern ' ', a single space in quotes, means split like awk: skip leading whitespace, split on runs of whitespace, and drop trailing whitespace. It is the right call for columns of text.

my $row = " alice 42 admin "; my @cols = split ' ', $row; print "@cols\n"; # alice 42 admin

perl demo.pl

alice 42 admin

Compare that with /\s+/, which leaves an empty first field when the line starts with a space. For command output, the quoted space is almost always the one you want.

join

join is the inverse: a separator and a list become one string. It is the safe way to build CSV-ish output, because you control the separator and the number of fields.

my @row = ("bob", 28, "dev"); my $line = join ",", @row; print "$line\n"; # bob,28,dev print join("\n", @row), "\n";

perl demo.pl

bob,28,dev
bob
28
dev

A round trip

split and join pair up in real scripts: read a line, change one field, write the line back. The example below also shows why a limit of 2 is useful.

my @cfg = split /\s*=\s*/, "timeout = 30", 2; $cfg[1] = $cfg[1] * 2; print join("=", @cfg), "\n"; # timeout=60

perl demo.pl

timeout=60

For real CSV with quoted commas, split is not enough. Lesson 8.1 shows Text::CSV, which handles the quoting rules for you.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does the pattern of split /:/, $line describe?

    Answer

    The separator between the fields, not the fields themselves

    split cuts wherever the pattern matches, so the pattern is the separator between the fields.

  2. What does split /,/, "a,b,," return by default?

    Answer

    Two fields, because trailing empty fields are dropped

    The default drops trailing empty fields, which is convenient for loose input and wrong for fixed columns.

  3. What is a negative third argument, as in split /,/, $csv, -1, for?

    Answer

    It keeps every empty field, including the trailing ones

    The negative limit means no field is thrown away, which a fixed-column format needs.

  4. What does a positive limit such as split /,/, $line, 2 do?

    Answer

    It caps the field count at two, and the rest of the line stays in the second field

    That is how a line is split into a key and its value only once.

  5. What does split ' ', $row do differently from split /\s+/, $row?

    Answer

    It skips leading whitespace and drops trailing whitespace, like awk; /\s+/ leaves an empty first field

    The quoted single space is the special awk-like form, which is almost always what command output needs.

  6. When is split the wrong tool for a CSV line?

    Answer

    When a field may contain the separator inside quotes, because Text::CSV handles that

    Quoted commas need a real CSV parser, which lesson 8.1 introduces.

  7. What does this program print? my @f = split /:/, "a:b:c"; print scalar @f, "\n";

    Answer

    3

    Two separators make three fields, and scalar asks for the count.

  8. What does this program print? my @f = split /,/, "a,b,,", -1; print scalar @f, "\n";

    Answer

    4

    The negative limit keeps the two trailing empty fields, so all four are counted.

  9. What does this program print? my @c = split ' ', " a b "; print "@c\n";

    Answer

    a b

    The special space form skips the leading and trailing whitespace and splits on the runs between the words.

  10. What does this program print? my @c = split /,/, "k=v,x,y", 2; print "$c[1]\n";

    Answer

    x,y

    The limit of two stops the split at the first comma, so the rest of the line stays in the second field.

Article author: Arthur Isaev