Using Perl to Process Data Saved in Tab-Delineated Text Files

The code below presents the basic structure of a Perl script that reads the columns of a tab-delineated text file and saves each column to a separate data array that can then be used to for additional processing. The code is set up to process a data file with two columns of data (x and y data) but can be easily modified to parse files with more than two columns.


# This Perl script takes a text file with two columns of 
# tab-delineated data and reads it in one line at a time 
# saving the values in each column to a separate array.

# If your data file has more than two columns representing 
# additional dimensions of a data set, simply add more
# variables and arrays to this code to hold this additional data.

# Define arrays to hold your data values. If you have more 
# than two columns of data, simply add more arrays.
@xData = ();
@yData = ();

# Replace data.txt with the name of your data file.
open(FILEIN,"<data.txt") or die ("Can't open the data file .\n");

while(<FILEIN>) { # Read in the line of the data file one at a time.

chomp($_); # Remove the carriage return from the current line 
# if present.

($xValue, $yValue) = split("\t", $_); # Separate the columns of data
# in the file at the tab(s). If your data has more than two columns,
# simply add more variables to hold the data.

# Save the data into arrays. Add more arrays if you have more than
# two columns of data.
$xData[$ii] = $xValue;
$yData[$ii] = $yValue; 


Leave a Reply

Your email address will not be published.