Commit | Line | Data |
9e609156 |
1 | <?xml version="1.0" encoding="utf-8"?> |
2 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.0//EN" |
3 | "http://www.w3.org/TR/xhtml-basic/xhtml-basic10.dtd"> |
4 | <HTML xmlns="http://www.w3.org/1999/xhtml" lang="en-US"> |
5 | <HEAD> |
6 | |
7 | <TITLE></TITLE> |
8 | <LINK REL="stylesheet" HREF="book.css" TYPE="text/css"> |
9 | |
10 | </HEAD> |
11 | |
12 | |
13 | <BODY id="" > |
14 | <div id="header_enclosure"> |
15 | |
16 | |
17 | |
18 | |
19 | |
20 | <H1> |
21 | <div align="center" id="page_header_title">David Toal's Homework |
22 | </div> |
23 | </H1> |
24 | |
25 | |
26 | |
27 | <A id=prev_link href="page-0101.html">Prev</A> |
28 | |
29 | |
30 | |
31 | <A id=next_link href="page-0103.html">Next</A> |
32 | |
33 | |
34 | |
35 | <A id=index_link href="index.html">Index</A> |
36 | |
37 | |
38 | |
39 | |
40 | |
41 | |
42 | |
43 | <HR> |
44 | |
45 | |
46 | |
47 | </div> |
48 | |
49 | <div id="body_enclosure"> |
50 | |
51 | |
52 | |
53 | |
54 | <UL> |
55 | |
56 | |
57 | <div class="item"> |
58 | <LI> |
59 | |
60 | <div class="item_title"></div> |
61 | |
62 | |
63 | |
64 | |
65 | |
66 | |
67 | |
68 | |
69 | |
70 | |
71 | <div class="code"> |
72 | <font size=+1><PRE> |
73 | |
74 | |
75 | package ArrayUtils; |
76 | |
77 | |
78 | use Exporter; |
79 | |
80 | @ISA = qw( Exporter ); |
81 | |
82 | |
83 | use FindArrayMax qw( &find_max_val &find_max_row ); |
84 | |
85 | @EXPORT = qw( &load_array &find_max_val &find_max_row ); |
86 | # if this is commented out, only fully qualified calls work |
87 | |
88 | @EXPORT_OK = qw( load_array find_max_val find_max_row ); |
89 | |
90 | |
91 | sub load_array { |
92 | |
93 | print "ArrayUtils::load_array\n"; |
94 | |
95 | my $fh = shift; |
96 | |
97 | my @array; |
98 | |
99 | while (<$fh>) { |
100 | |
101 | # print "read from file $_"; |
102 | |
103 | my @values = split; |
104 | |
105 | push @array, \@values; |
106 | |
107 | } |
108 | |
109 | return \@array; |
110 | |
111 | } |
112 | |
113 | |
114 | 1; |
115 | |
116 | |
117 | |
118 | package ArrayUtils; |
119 | |
120 | |
121 | use Exporter; |
122 | |
123 | @ISA = qw( Exporter ); |
124 | |
125 | |
126 | @EXPORT_OK = qw( &find_max_val &find_max_row ); |
127 | |
128 | |
129 | sub find_max_val { |
130 | |
131 | print "ArrayUtils::find_max_val\n"; |
132 | |
133 | my $matrix = shift; |
134 | |
135 | my $max_val; |
136 | |
137 | foreach my $row (@{$matrix}) { |
138 | foreach my $val (@{$row}) { |
139 | $max_val = $val if ($val > $max_val); |
140 | } |
141 | } |
142 | |
143 | return $max_val; |
144 | |
145 | } |
146 | |
147 | |
148 | |
149 | sub find_max_row { |
150 | |
151 | print "ArrayUtils::find_max_row\n"; |
152 | |
153 | my $matrix = shift; |
154 | |
155 | my $max_row; |
156 | my $max_val; |
157 | |
158 | foreach my $row (@{$matrix}) { |
159 | foreach my $val (@{$row}) { |
160 | $max_row = $row, $max_val = $val if ($val > $max_val); |
161 | } |
162 | } |
163 | |
164 | return $max_row; |
165 | |
166 | } |
167 | |
168 | |
169 | 1; |
170 | |
171 | |
172 | |
173 | package HashDispatch; |
174 | |
175 | use Exporter; |
176 | |
177 | @ISA = qw( Exporter ); |
178 | |
179 | @EXPORT = qw( &dispatch_table ); |
180 | |
181 | |
182 | my $DEBUG = 1; |
183 | print "HashDispatch::DEBUG set\n" if ($DEBUG); |
184 | |
185 | sub dispatch_table { |
186 | |
187 | print "HashDispatch::dispatch_table\n" if ($DEBUG); |
188 | |
189 | my $dispatch = { |
190 | 'stooge' => \&stooges , |
191 | 'marx' => \&marxes |
192 | }; |
193 | |
194 | return $dispatch; |
195 | |
196 | } |
197 | |
198 | |
199 | sub stooges { |
200 | my ($name, $team) = @_; |
201 | return "$name, last name is $team->{'stooge'}->{$name}"; |
202 | } |
203 | |
204 | |
205 | sub marxes { |
206 | my ($name, $team) = @_; |
207 | return "$name, real name is $team->{'marx'}->{$name}"; |
208 | } |
209 | |
210 | 1; |
211 | |
212 | |
213 | |
214 | # my $dispatch = { |
215 | # 'stooge' => \sub { my ($name, $hash) = @_; print "stooge name is $name\n"; } , |
216 | # 'marx' => \sub { my ($name, $hash) = @_; print "marx name is $name\n"; } |
217 | # }; |
218 | |
219 | |
220 | |
221 | package HashUtils; |
222 | |
223 | use FileHandle; |
224 | |
225 | use Exporter; |
226 | |
227 | @ISA = qw( Exporter ); |
228 | |
229 | |
230 | use HashDispatch; |
231 | |
232 | @EXPORT = qw( &load_hash &dispatch_table ); |
233 | |
234 | # @EXPORT_OK = qw( &load_hash &dispatch_table ); |
235 | |
236 | |
237 | |
238 | my $DEBUG = 1; |
239 | |
240 | print "HashUtils::DEBUG set\n" if ($DEBUG); |
241 | |
242 | |
243 | sub load_hash { |
244 | |
245 | print "HashUtils::load_hash\n" if ($DEBUG); |
246 | |
247 | my $fh = shift; |
248 | |
249 | my %hash; |
250 | |
251 | while (<$fh>) { |
252 | |
253 | print "line is $_" if ($DEBUG); |
254 | |
255 | my ($team, $first, $second) = split; |
256 | |
257 | $hash{$team}->{$first} = $second; |
258 | |
259 | } |
260 | |
261 | return \%hash; |
262 | |
263 | } |
264 | |
265 | |
266 | 1; |
267 | |
268 | 4 11 7 |
269 | 9 15 6 2 |
270 | 1 9 |
271 | |
272 | |
273 | use strict; |
274 | |
275 | use FileHandle; |
276 | |
277 | use ArrayUtils; |
278 | |
279 | |
280 | my $filename = "array-data.txt"; |
281 | |
282 | |
283 | my $fh = FileHandle->new(); |
284 | |
285 | $fh->open("< $filename"); |
286 | |
287 | |
288 | my $matrix = load_array($fh); |
289 | |
290 | foreach my $row (@{$matrix}) { |
291 | foreach my $val (@{$row}) { print "$val, "; } |
292 | print "\n"; |
293 | } |
294 | |
295 | |
296 | my $max_val = find_max_val($matrix); |
297 | |
298 | print "max val is $max_val\n"; |
299 | |
300 | |
301 | my $max_row = find_max_row($matrix); |
302 | |
303 | print "max row is: "; |
304 | |
305 | foreach my $val (@{$max_row}) { |
306 | print "$val, "; |
307 | } |
308 | |
309 | print "\n"; |
310 | |
311 | |
312 | |
313 | use strict; |
314 | |
315 | use FileHandle; |
316 | |
317 | use ArrayUtils qw( &load_array &find_max_val &find_max_row ); |
318 | |
319 | |
320 | my $filename = "array-data.txt"; |
321 | |
322 | |
323 | my $fh = FileHandle->new(); |
324 | |
325 | $fh->open("< $filename"); |
326 | |
327 | |
328 | my $matrix = ArrayUtils::load_array($fh); |
329 | |
330 | foreach my $row (@{$matrix}) { |
331 | foreach my $val (@{$row}) { print "$val, "; } |
332 | print "\n"; |
333 | } |
334 | |
335 | |
336 | my $max_val = ArrayUtils::find_max_val($matrix); |
337 | |
338 | print "max val is $max_val\n"; |
339 | |
340 | |
341 | my $max_row = ArrayUtils::find_max_row($matrix); |
342 | |
343 | print "max row is: "; |
344 | |
345 | foreach my $val (@{$max_row}) { |
346 | print "$val, "; |
347 | } |
348 | |
349 | print "\n"; |
350 | |
351 | marx groucho julius |
352 | stooge moe howard |
353 | stooge larry fine |
354 | marx chico leonard |
355 | marx harpo arthur |
356 | stooge curly howard |
357 | |
358 | |
359 | use strict; |
360 | |
361 | use Data::Dumper; |
362 | |
363 | use FileHandle; |
364 | |
365 | use HashUtils; |
366 | |
367 | |
368 | my $filename = "hash-data.txt"; |
369 | |
370 | my $fh = FileHandle->new(); |
371 | |
372 | $fh->open("< $filename"); |
373 | |
374 | |
375 | my $team = load_hash($fh); |
376 | |
377 | print Dumper $team; |
378 | |
379 | |
380 | my $table = dispatch_table(); |
381 | |
382 | foreach my $team_name (keys(%{$team})) { |
383 | print "team name is $team_name\n"; |
384 | |
385 | foreach my $name (keys(%{ $team->{$team_name} })) { |
386 | my $result = $table->{$team_name}->($name, $team); |
387 | print "$result\n"; |
388 | } |
389 | |
390 | } |
391 | |
392 | |
393 | |
394 | use strict; |
395 | |
396 | use Data::Dumper; |
397 | |
398 | use FileHandle; |
399 | |
400 | use HashUtils qw( &load_hash &dispatch_table ); |
401 | |
402 | |
403 | my $filename = "hash-data.txt"; |
404 | |
405 | my $fh = FileHandle->new(); |
406 | |
407 | $fh->open("< $filename"); |
408 | |
409 | |
410 | my $team = HashUtils::load_hash($fh); |
411 | |
412 | print Dumper $team; |
413 | |
414 | |
415 | my $table = HashUtils::dispatch_table(); |
416 | |
417 | foreach my $team_name (keys(%{$team})) { |
418 | print "team name is $team_name\n"; |
419 | |
420 | foreach my $name (keys(%{ $team->{$team_name} })) { |
421 | my $result = $table->{$team_name}->($name, $team); |
422 | print "$result\n"; |
423 | } |
424 | |
425 | } |
426 | |
427 | |
428 | |
429 | use strict; |
430 | |
431 | use Data::Dumper; |
432 | |
433 | use FileHandle; |
434 | |
435 | use HashUtils; |
436 | |
437 | |
438 | my $DEBUG = 1; |
439 | print "main::DEBUG set\n" if ($DEBUG); |
440 | |
441 | |
442 | my $filename = "hash-data.txt"; |
443 | |
444 | my $fh = FileHandle->new(); |
445 | |
446 | $fh->open("< $filename"); |
447 | |
448 | |
449 | my $team = load_hash($fh); |
450 | |
451 | print Dumper $team; |
452 | |
453 | |
454 | my $table = dispatch_table(); |
455 | |
456 | foreach my $team_name (keys(%{$team})) { |
457 | print "team name is $team_name\n"; |
458 | |
459 | foreach my $name (keys(%{ $team->{$team_name} })) { |
460 | my $result = $table->{$team_name}->($name, $team); |
461 | print "$result\n"; |
462 | } |
463 | |
464 | } |
465 | |
466 | </PRE></font> |
467 | </div> |
468 | |
469 | |
470 | </LI> |
471 | </div> |
472 | |
473 | |
474 | </UL> |
475 | |
476 | |
477 | |
478 | </div> |
479 | |
480 | <div id="footer_enclosure"> |
481 | |
482 | |
483 | |
484 | <HR> |
485 | |
486 | |
487 | <A id=prev_link href="page-0101.html">Prev</A> |
488 | |
489 | |
490 | |
491 | <A id=next_link href="page-0103.html">Next</A> |
492 | |
493 | |
494 | |
495 | <A id=index_link href="index.html">Index</A> |
496 | |
497 | |
498 | |
499 | |
500 | |
501 | |
502 | |
503 | |
504 | |
505 | |
506 | |
507 | </div> |
508 | </BODY> |
509 | |
510 | </HTML> |
511 | |