eTechtips

Tech Tips and Insights

Perl: Open and read file line by line

I regularly have to process Comma Seperated Value or CSV files, usually for user lists. So the following perl code is a framework for processing a file line by line.

#!/usr/bin/perl
use strict;
my $line;

open(FILEIN, "filename.txt");

while(<FILEIN>)
{
  chomp();
  $line = $_;
  # Process $line for data
  # Example seperating line by commas: 
  my ($username,$firstname,$lastname,$email) = split(/,/,$line);
}

close(FILEIN);

Loading

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.