Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
parse_csv_example.cc
Go to the documentation of this file.
1
25#include <iostream>
26#include <sstream>
27#include <parse-csv.H>
28
29using namespace std;
30using namespace Aleph;
31
32int main()
33{
34 cout << "=== CSV Parsing: Educational Examples ===\n\n";
35
36 // =========================================================================
37 // EXAMPLE 1: Basic CSV Reading
38 // =========================================================================
39 // CONCEPT: Parse simple CSV data into rows and fields
40 {
41 cout << "--- Example 1: Basic CSV Parsing ---\n\n";
42
43 // Sample CSV data (typically from file)
44 stringstream csv_data(
45 "name,age,city\n"
46 "Alice,30,NYC\n"
47 "Bob,25,LA\n"
48 "Charlie,35,Chicago\n"
49 );
50
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";
56
57 // STEP 1: Create CSV reader
59
60 // STEP 2: Read header row
61 Array<string> header = reader.read_header();
62 cout << "Header columns: " << header.size() << "\n";
63
64 // STEP 3: Read data rows
65 cout << "\nParsing rows:\n";
66 int row_num = 1;
67 while (reader.has_next())
68 {
69 CsvRow row = reader.next_row();
70
71 // Access by column name
72 cout << " Row " << row_num++ << ": "
73 << row["name"] << ", age " << row["age"]
74 << ", from " << row["city"] << "\n";
75 }
76
77 cout << "\nKEY FEATURE: Access fields by name, not index!\n";
78 cout << " More readable and maintainable code\n\n";
79 }
80
81 // =========================================================================
82 // EXAMPLE 2: Type Conversion
83 // =========================================================================
84 // CONCEPT: CSV fields are strings, but we often need numbers
85 {
86 cout << "--- Example 2: Type Conversion ---\n\n";
87
88 stringstream csv_data(
89 "product,price,quantity\n"
90 "Widget,19.99,100\n"
91 "Gadget,29.99,50\n"
92 "Doohickey,9.99,200\n"
93 );
94
96 reader.read_header();
97
98 cout << "Calculating total inventory value...\n\n";
99
100 double total_value = 0.0;
101 while (reader.has_next())
102 {
103 CsvRow row = reader.next_row();
104
105 // Convert string fields to numbers
106 double price = row.get<double>("price");
107 int quantity = row.get<int>("quantity");
108 double item_value = price * quantity;
109
111
112 cout << " " << row["product"]
113 << ": $" << price << " x " << quantity
114 << " = $" << item_value << "\n";
115 }
116
117 cout << "\nTotal inventory value: $" << total_value << "\n";
118 cout << "\nTYPE SAFETY: get<T>() throws exception if conversion fails\n";
119 cout << " Catches data errors early\n\n";
120 }
121
122 // =========================================================================
123 // EXAMPLE 3: Handling Quoted Fields
124 // =========================================================================
125 // CONCEPT: CSV fields with commas/quotes must be escaped
126 {
127 cout << "--- Example 3: Quoted Fields (RFC 4180) ---\n\n";
128
129 // CSV with quoted fields containing commas and quotes
130 stringstream csv_data(
131 "name,description,price\n"
132 "\"Smith, John\",\"Consultant, Senior\",150\n"
133 "\"O'Brien, Mary\",\"Director, \"\"Special\"\" Projects\",200\n"
134 );
135
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";
140
142 reader.read_header();
143
144 cout << "Parsed correctly:\n";
145 while (reader.has_next())
146 {
147 CsvRow row = reader.next_row();
148 cout << " Name: " << row["name"] << "\n";
149 cout << " Role: " << row["description"] << "\n";
150 cout << " Rate: $" << row["price"] << "/hr\n\n";
151 }
152
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";
157 }
158
159 // =========================================================================
160 // EXAMPLE 4: Filtering Data
161 // =========================================================================
162 // CONCEPT: Select subset of rows based on criteria
163 {
164 cout << "--- Example 4: Filtering Data ---\n\n";
165
166 stringstream csv_data(
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"
173 );
174
175 // Read all data
177
178 cout << "Total employees: " << (all_rows.size() - 1) << "\n\n";
179
180 // Filter: Only Engineering department
181 cout << "Engineering employees:\n";
182 auto eng_filter = [](const Array<string>& row) {
183 return row.size() > 1 && row[1] == "Engineering";
184 };
185
187
188 // Skip header, show results
189 for (size_t i = 1; i < eng_rows.size(); ++i)
190 {
191 cout << " " << eng_rows[i][0]
192 << " - $" << eng_rows[i][2] << "\n";
193 }
194
195 cout << "\nPOWERFUL FEATURE: Lambda-based filtering\n";
196 cout << " Can combine multiple conditions\n\n";
197 }
198
199 // =========================================================================
200 // EXAMPLE 5: Data Analysis
201 // =========================================================================
202 // CONCEPT: Compute statistics from CSV data
203 {
204 cout << "--- Example 5: Data Analysis ---\n\n";
205
206 stringstream csv_data(
207 "month,revenue,expenses\n"
208 "Jan,50000,30000\n"
209 "Feb,55000,32000\n"
210 "Mar,48000,31000\n"
211 "Apr,62000,35000\n"
212 );
213
215 reader.read_header();
216
217 double total_revenue = 0.0;
218 double total_expenses = 0.0;
219 int months = 0;
220
221 cout << "Monthly P&L:\n";
222 cout << "Month | Revenue | Expenses | Profit\n";
223 cout << "------|---------|----------|--------\n";
224
225 while (reader.has_next())
226 {
227 CsvRow row = reader.next_row();
228
229 double revenue = row.get<double>("revenue");
230 double expenses = row.get<double>("expenses");
231 double profit = revenue - expenses;
232
233 total_revenue += revenue;
235 months++;
236
237 cout << row["month"] << " | $" << revenue
238 << " | $" << expenses << " | $" << profit << "\n";
239 }
240
241 cout << "\nSUMMARY:\n";
242 cout << " Total Revenue: $" << total_revenue << "\n";
243 cout << " Total Expenses: $" << total_expenses << "\n";
244 cout << " Net Profit: $" << (total_revenue - total_expenses) << "\n";
245 cout << " Avg Monthly Revenue: $" << (total_revenue / months) << "\n\n";
246
247 cout << "REAL-WORLD: Financial reporting, data analytics dashboards\n\n";
248 }
249
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";
274
275 return 0;
276}
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:138
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
Definition tpl_array.H:333
Lazy CSV reader for large files.
Definition parse-csv.H:711
A CSV row with header-based field access.
Definition parse-csv.H:601
size_t size() const noexcept
Count the number of elements of the list.
Definition htlist.H:1319
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
Array< Array< std::string > > csv_filter(const Array< Array< std::string > > &rows, Pred predicate)
Filter CSV rows by a predicate.
Definition parse-csv.H:859
Array< Array< std::string > > csv_read_all(std::istream &in, char delimiter=',')
Read all rows from a CSV input stream.
Definition parse-csv.H:253
DynList< T > maps(const C &c, Op op)
Classic map operation.
STL namespace.
Comprehensive CSV (Comma-Separated Values) parsing and manipulation utilities.
int main()