jkeenan1 has asked for the wisdom of the Perl Monks concerning the following question:
NAME RANK SOCIAL_SECURITY George Washington 000-00-0000 John Adams 000-00-0001
What distinguishes one such data file from another is the header row: both the particular fields indicated as well as the total number of fields. (The files may even be identically named, differentiated only by a timestamp of their arrival time in my system. So the content of the header row is crucial.) Hence, the header row has to be treated differently from all other rows.
A typical approach to this is as follows: First, initialize a flag to a false value, begin to read the file line by line, process the header row to see which fields are present, set the flag to a true value, then process all remaining rows.
$header_seen = 0; while (<>) { unless ($header_seen) { # process header to get field names $header_seen++; } else { # process each subsequent record } }
This requires that I check the status of $header_seen on each line of the file. I suppose that I could use Tie::File and process row 0 differently from all others ... but this is likely to be slower and less memory efficient.
Is there any other approach to this problem?
Thank you very much.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Line-by-line processing of a file where the first line is different
by xdg (Monsignor) on Jul 11, 2006 at 11:24 UTC | |
by jkeenan1 (Deacon) on Jul 11, 2006 at 11:28 UTC | |
|
Re: Line-by-line processing of a file where the first line is different
by Corion (Patriarch) on Jul 11, 2006 at 11:27 UTC | |
by jkeenan1 (Deacon) on Jul 11, 2006 at 11:32 UTC | |
|
Re: Line-by-line processing of a file where the first line is different
by Ieronim (Friar) on Jul 11, 2006 at 13:05 UTC | |
|
Re: Line-by-line processing of a file where the first line is different
by GrandFather (Saint) on Jul 11, 2006 at 22:20 UTC |