34 cout <<
"=== CSV Parsing: Educational Examples ===\n\n";
41 cout <<
"--- Example 1: Basic CSV Parsing ---\n\n";
48 "Charlie,35,Chicago\n"
51 cout <<
"CSV Data:\n";
52 cout <<
"name,age,city\n";
53 cout <<
"Alice,30,NYC\n";
54 cout <<
"Bob,25,LA\n";
55 cout <<
"Charlie,35,Chicago\n\n";
62 cout <<
"Header columns: " << header.
size() <<
"\n";
65 cout <<
"\nParsing rows:\n";
73 <<
row[
"name"] <<
", age " <<
row[
"age"]
74 <<
", from " <<
row[
"city"] <<
"\n";
77 cout <<
"\nKEY FEATURE: Access fields by name, not index!\n";
78 cout <<
" More readable and maintainable code\n\n";
86 cout <<
"--- Example 2: Type Conversion ---\n\n";
89 "product,price,quantity\n"
92 "Doohickey,9.99,200\n"
98 cout <<
"Calculating total inventory value...\n\n";
118 cout <<
"\nTYPE SAFETY: get<T>() throws exception if conversion fails\n";
119 cout <<
" Catches data errors early\n\n";
127 cout <<
"--- Example 3: Quoted Fields (RFC 4180) ---\n\n";
131 "name,description,price\n"
132 "\"Smith, John\",\"Consultant, Senior\",150\n"
133 "\"O'Brien, Mary\",\"Director, \"\"Special\"\" Projects\",200\n"
136 cout <<
"CSV with special characters:\n";
137 cout <<
"name,description,price\n";
138 cout <<
"\"Smith, John\",\"Consultant, Senior\",150\n";
139 cout <<
"\"O'Brien, Mary\",\"Director, \"\"Special\"\" Projects\",200\n\n";
144 cout <<
"Parsed correctly:\n";
148 cout <<
" Name: " <<
row[
"name"] <<
"\n";
149 cout <<
" Role: " <<
row[
"description"] <<
"\n";
150 cout <<
" Rate: $" <<
row[
"price"] <<
"/hr\n\n";
153 cout <<
"RFC 4180 RULES:\n";
154 cout <<
" 1. Fields with commas → enclosed in quotes\n";
155 cout <<
" 2. Quotes inside field → doubled \"\"\n";
156 cout <<
" 3. Parser handles this automatically!\n\n";
164 cout <<
"--- Example 4: Filtering Data ---\n\n";
167 "employee,department,salary\n"
168 "Alice,Engineering,80000\n"
169 "Bob,Marketing,60000\n"
170 "Charlie,Engineering,90000\n"
171 "Diana,Sales,70000\n"
172 "Eve,Engineering,85000\n"
181 cout <<
"Engineering employees:\n";
183 return row.
size() > 1 &&
row[1] ==
"Engineering";
192 <<
" - $" <<
eng_rows[i][2] <<
"\n";
195 cout <<
"\nPOWERFUL FEATURE: Lambda-based filtering\n";
196 cout <<
" Can combine multiple conditions\n\n";
204 cout <<
"--- Example 5: Data Analysis ---\n\n";
207 "month,revenue,expenses\n"
217 double total_revenue = 0.0;
221 cout <<
"Monthly P&L:\n";
222 cout <<
"Month | Revenue | Expenses | Profit\n";
223 cout <<
"------|---------|----------|--------\n";
238 <<
" | $" <<
expenses <<
" | $" << profit <<
"\n";
241 cout <<
"\nSUMMARY:\n";
242 cout <<
" Total Revenue: $" << total_revenue <<
"\n";
245 cout <<
" Avg Monthly Revenue: $" << (total_revenue /
months) <<
"\n\n";
247 cout <<
"REAL-WORLD: Financial reporting, data analytics dashboards\n\n";
250 cout <<
"=== SUMMARY: CSV Best Practices ===\n";
251 cout <<
"\n1. ALWAYS READ HEADERS:\n";
252 cout <<
" Use reader.read_header() before processing rows\n";
253 cout <<
" Access by name: row[\"column\"] not row[index]\n";
254 cout <<
"\n2. TYPE CONVERSION:\n";
255 cout <<
" Use row.get<T>() for type-safe conversion\n";
256 cout <<
" Catches invalid data early with exceptions\n";
257 cout <<
"\n3. HANDLE SPECIAL CHARACTERS:\n";
258 cout <<
" Parser automatically handles RFC 4180:\n";
259 cout <<
" - Quoted fields with commas\n";
260 cout <<
" - Escaped quotes (\"\")\n";
261 cout <<
" - Line breaks in fields\n";
262 cout <<
"\n4. MEMORY EFFICIENCY:\n";
263 cout <<
" Use CsvReader for streaming (large files)\n";
264 cout <<
" Use csv_read_all() for small datasets\n";
265 cout <<
"\n5. COMMON OPERATIONS:\n";
266 cout <<
" - Filter: csv_filter() with lambda\n";
267 cout <<
" - Sort: csv_sort_by_column_numeric()\n";
268 cout <<
" - Join: csv_inner_join() on key column\n";
269 cout <<
" - Group: csv_group_by() for aggregation\n";
270 cout <<
"\n6. ERROR HANDLING:\n";
271 cout <<
" - Wrap file reads in try-catch\n";
272 cout <<
" - Check row.size() before access\n";
273 cout <<
" - Validate data types with get<T>()\n";
Simple dynamic array with automatic resizing and functional operations.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
Lazy CSV reader for large files.
A CSV row with header-based field access.
size_t size() const noexcept
Count the number of elements of the list.
Main namespace for Aleph-w library functions.
Array< Array< std::string > > csv_filter(const Array< Array< std::string > > &rows, Pred predicate)
Filter CSV rows by a predicate.
Array< Array< std::string > > csv_read_all(std::istream &in, char delimiter=',')
Read all rows from a CSV input stream.
DynList< T > maps(const C &c, Op op)
Classic map operation.
Comprehensive CSV (Comma-Separated Values) parsing and manipulation utilities.