

A warning about CSV filesĪs I note in one of my Scala books, you have to be careful with CSV files because some people have a loose definition of what “CSV” means to them. In this case I'm just printing out the data, but obviously you'll want to do something else with your data. Once you have the four fields of information as shown above, you can do whatever else you want to do with the data. Simply stated, this line says "Using the Perl split function, take the current line of information, represented by $line, and split it into four fields, where each field is separated by a comma." In our Perl program, the line that looks like this:ĭoes the "Perl split string" work, splitting each line of data (each string) into four separate fields. Print "$field1 : $field2 : $field3 : $field4"

Open (F, $file) || die ("Could not open $file!") Here’s a simple Perl program that can be used to read a file named input.csv and split the strings that are read into different field (so this is also a Perl split string example): The following example shows how this works. You can easily split the data into four fields using the Perl split function. So, how do you read the data? Solution: The Perl split functionĪ comma-separated data file looks like this: The width of each column can vary, but the column data is guaranteed not to contain any columns itself, so it's safe to think of the comma as truly being a column separator. Problem: You have a file that contains columns of data, and each column is separated by a comma (a CSV file). Perl FAQ: How do I read a CSV file in Perl?
