Perl split function - How to process pipe-delimited text files

When writing Perl programs, many "data" files you end up working with are really plain text files that use some kind of character to act as a field delimiter. For instance, a program I was working with recently reads data from files whose fields are separated by the pipe character ("|").

In Perl programs these files are easy to work with. You just open the text file as usual, read each line in the file inside a Perl while loop, then use the Perl split function to split the current line with the known field delimiter, in this case the pipe character.

Example Perl script

Here's a sample Perl program that does just that; it reads each line from a file, separates the fields in each line by the pipe character, and assigns each column (field) to an array named @fields:

#!/usr/bin/perl

# purpose:   read a pipe-delimited text file (i.e., the "fields" are separated by
#            the "|" character
# usage:     perl read-pipe-file.pl

# a sample record looks like this: 
# field1 data | field2 data | field 3 | and so on ...

$filename = 'my-data-file.txt';

# use the perl open function to open the file
open(FILE, $filename) or die "Could not read from $filename, program halting.";

# loop through each line in the file
# with the typical "perl file while" loop
while(<FILE>)
{
  # get rid of the pesky newline character
  chomp;

  # read the fields in the current line into an array
  @fields = split('|', $_);

  # print the first field
  print "$fields[0]\n";
}
close FILE;

As you can see from this Perl program, once you have the fields of each line stored in an array, it's a simple matter to do something with each field. In my case I'm just printing the value of the first field of each row, but you can do whatever you need to do with the data in your own Perl programs.