[GRLUG] Some perl help

Adam Tauno Williams awilliam at whitemice.org
Sat Jul 2 16:35:33 EDT 2011


David Pembrook <david at pembrook.net> wrote:
>I'm fluent enough in perl to be dangerous.  I have a line from a csv 
>file and I want the "columns" in an array without any quotes. Some 
>columns have them, some don't so I can't count on their presence.
>The quick and dirty way I came up with used a temp array and a counter 
>and know that has to be a better way to handle this.
>         while (<FILE>) {
>             @temp_values2 = split(",", $_);

String splitting is an evil and wrong way to read a delimited record;
doing so will fail in a fantastic variety of ways.   I'm not a Perl
developer but I'm certain Perl must provide a standard module for
reading delimited data sets.  In Python this would be the "csv" module -
which allows you to specify field delimiter, line terminator, and field
quoting style.   Don't forget that text files also have encoding - one
cannot assume text data is ASCII (especially if a user can get anywhere
near it using cut-n-paste).   If it isn't straight-ASCII your string
splitting will fail in mysterious cases that on visual inspection look
fine.

-- 
Adam Tauno Williams


-- 
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.



More information about the grlug mailing list