Read perl file line by line


















What if this is an optional configuration file? If you can read it you change some settings, if you cannot read you just use the defaults. In that case the second solution might be a better way to write your code. If it is true we go ahead and read the content of the file. If it failed we give a warning using the built-in warn function but don't throw an exception.

Toggle navigation Perl Maven. Standard output, standard error and command line redirection Warning when something goes wrong What does die do? Unknown warnings category Can't use string Symbolic references in Perl Can't locate Can't locate object method " There are a number of different ways of reading a file. The example below reads one line from the file and stores it in the scalar. If there was an error or the filehandle is at end of the file, then it returns undef.

On the success of file reading, the function returns the number of bytes read, zero at end of file, or undef if there was an error. Example: File. Give a warning When filehandle could not be assigned a valid file pointer it just prints warning message using warn function and keeps running.

Skip to content. Change Language. Related Articles. Control Flow. OOP Concepts. Collectives on Stack Overflow. Learn more. Perl read line by line Ask Question. Asked 11 years ago. Active 5 years, 11 months ago. Viewed k times. Add a comment. Active Oldest Votes. Greg Bacon k 29 29 gold badges silver badges bronze badges.

Evan Carroll Evan Carroll NoobEditor Doon Doon Yes, you are right. Reading and processing text files is one of the common tasks done by Perl. For example, often you encounter a CSV file where CSV stand for Comma-separated values and you need to extract some information from there.

Here is an example with three solutions. Good, Better, Best. The first is a reasonable solution for simple CSV files, that does not require anything beyond Perl. The second fixes some problems caused by a slightly more complex CSV files. The third is probably the best solution.

The price is that these solutions depend on a module from CPAN. Pick the one that matches your needs. In each row there are fields separated with comma. Of course the separator can be any character as long as it is the same in the whole file. Anyway, the task was to summarize the number in the 3rd column. The algorithm The process should go like this: Read in the file line by line.

For each line, extract the 3rd column. Add the value to a central variable where we accumulate the sum. We have already learned earlier how to read a file line by line so we only need to know how to process each row and how to extract the 3rd column. I cannot use substr easily as the location of the 3rd field is changing. What is fixed is that it is between the 2nd and the 3rd comma. I could use index 3 times on each row to locate the 2nd and the 3rd comma, and then use substr but Perl has a much easier way for this.



0コメント

  • 1000 / 1000