Records and fields¶
One line: A data file is lines, a line is fields, and fields hold names and numbers — and at each step the shortest Perl has a gap: chomp leaves Windows' \r, split does not know CSV quoting, sort does not know the alphabet, and + 0 reads 42abc as 42.
This is the chapter for the scripts that read somebody else's export. Each lesson takes one step of reading a file into records — the end of the line, the fields in it, the order they go in, and the numbers inside them — and shows the input that breaks the obvious code, then the code that does not break.
| Lesson | Level | What it settles |
|---|---|---|
chomp leaves the \r |
101 | The CRLF file whose last field never matches, four fixes tried on both kinds of file, and \R |
CSV is not split |
201 | A quoted comma, a doubled quote, an apostrophe and a quoted line break, through split and through Text::ParseWords |
sort is not alphabetical |
201 | Numbers, capitals, accents and Polish, from cmp through Unicode::Collate::Locale |
| Numbers from text | 201 | What + 0 accepts, hex and oct, a regex that really checks for an integer, and where floating point rounds |
Where this connects¶
- The encodings library's CRLF vs LF ↗, A BOM in a CSV ↗ and Sorting and collation ↗ are the same real-data failures, measured from the bytes up.
- The C library's Parsing a number from text ↗ is the number lesson with nothing done for you.
- The encodings library's Packing a record ↗ writes a binary record with Perl's
pack— the other kind of record.