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 :.
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. Perl split function - How to process pipe-delimited text files. By Alvin Alexander. Open the first file, then read each line into an array. Pop off the top 2 lines, as they are not data useful for the program.
For each line in the file: Read the first column, then add it to an array. You can do this by using split ; …. Generally there is no memory allocation necessary for perl scripts. Perl will use what memory is needed all they way to using all memory if thats what it takes. You can do this by using split ; Read the second column then add it to a second array. Read the third column then add it to a third array.
Open a new file, for the first column. Print the whole array into the file. Close the file. Open a second file, for the second column. Print the whole array into the second file. Open a third and last file, for the last column. Print the whole array into the third file. I was hoping for sample that would solve this simple case so that I might have a framework to look at when I solve the real problem.
Measure content performance. Develop and improve products. List of Partners vendors. Share Flipboard Email. By Kirk Brown Kirk Brown. Learn about our Editorial Process. Featured Video. Cite this Article Format. Brown, Kirk. Your Privacy Rights. In the same way you use a character to split, you can use a string. In some cases, you may want to split the string on a pattern regular expression or a type of character. We'll assume here that you know a little about regular expressions. In this example we will split on any integer:.
If you split on an undefined value, the string will be split on every character:. In this example we print 'aa' either side of the values so we can see where the split took place:. As you can see, it has split on the newlines that were in our data.
0コメント