Top Banner
Lecture 13 Tables from Arrays of Objects Exploding Strings * * Course logo spider web photograph from Morguefile openstock photograph by Gabor Karpati, Hungary.
18

Lecture 13 - cs.colostate.edu

Oct 16, 2021

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Lecture 13 - cs.colostate.edu

Lecture 13Tables from Arrays of Objects

Exploding Strings

*

* Course logo spider web photograph from Morguefile openstock photograph by Gabor Karpati, Hungary.

Page 2: Lecture 13 - cs.colostate.edu

2/19/17 Slide 22/19/17 Slide 2CSU CT 310 - Web Development ©Ross Beveridge & Jaime Ruiz

Example 5 combines:ObjectsArraysHTML Tables

Page 3: Lecture 13 - cs.colostate.edu

Example 05ØUse objects to manage informationØA state is a singular thing with 3

related data items:

2/19/17 Slide 3CSU CT 310 - Web Development ©Ross Beveridge & Jaime Ruiz

Page 4: Lecture 13 - cs.colostate.edu

2/19/17 Slide 42/19/17 Slide 4CSU CT 310 - Web Development ©Ross Beveridge & Jaime Ruiz

Punchline: Transform an array of objects to a table

Page 5: Lecture 13 - cs.colostate.edu

An Array of StatesØCreate three states in an arrayØNote line 41: array holds all the

member data for the first state.

2/19/17 Slide 5CSU CT 310 - Web Development ©Ross Beveridge & Jaime Ruiz

Page 6: Lecture 13 - cs.colostate.edu

More about get_object_varsØWith one function you can fully

equate object member data with a keyword style array.

ØAccess to the keys is very handy.

2/19/17 Slide 6CSU CT 310 - Web Development ©Ross Beveridge & Jaime Ruiz

Page 7: Lecture 13 - cs.colostate.edu

2/19/17 Slide 7

Keys Are Column Headers

2/19/17 Slide 7CSU CT 310 - Web Development ©Ross Beveridge & Jaime Ruiz

Here again is a part of the code.Line 44: Object member data to an arrayLine 44: Retrieve keywords from arrayLine 45: Iterate through keysLine 46: One table column header per key.

Page 8: Lecture 13 - cs.colostate.edu

Filling in the TableØEnumerate the values in the table

2/19/17 Slide 8CSU CT 310 - Web Development ©Ross Beveridge & Jaime Ruiz

Upper and lower versions function the same. Lower avoids intermediate variable

Page 9: Lecture 13 - cs.colostate.edu

2/19/17 Slide 9

Style

2/19/17 Slide 9CSU CT 310 - Web Development ©Ross Beveridge & Jaime Ruiz

Page 10: Lecture 13 - cs.colostate.edu

Now Without LoopsØSame goal, build a table from an

array of objects.ØBut, no loops, just array reduction

ØNested array reduction at that.

ØMany ways this could be done.Ø In this example, table header and

table data rows first built as strings.ØThen sent to the client, i.e. a succinct

echo command inside table tags.

2/19/17 Slide 10CSU CT 310 - Web Development ©Ross Beveridge & Jaime Ruiz

Page 11: Lecture 13 - cs.colostate.edu

2/19/17 Slide 11

Example 6 Code

2/19/17 Slide 11CSU CT 310 - Web Development ©Ross Beveridge & Jaime Ruiz

Page 12: Lecture 13 - cs.colostate.edu

2/19/17 Slide 12

Example 06 on Client

2/19/17 Slide 12CSU CT 310 - Web Development ©Ross Beveridge & Jaime Ruiz

Think of it this way. Map reduce can be very good at constructing repeated patterns from arrays.

Page 13: Lecture 13 - cs.colostate.edu

Example 06 ObservationsØExample 06 may be a bit extreme

ØOK to ask, who finds this more readable?ØOn the plus side

ØHighly functional limits side-effects.ØInternal function easy to modify.

ØPunchline for CT 3101. Study and understand Example 06!2. For now avoid loops when you can. 3. Once you master both styles, then you

will be able to make informed choices.

2/19/17 Slide 13CSU CT 310 - Web Development ©Ross Beveridge & Jaime Ruiz

Page 14: Lecture 13 - cs.colostate.edu

2/19/17 Slide 14

Manipulating Files/Strings

2/19/17 Slide 14CSU CT 310 - Web Development ©Ross Beveridge & Jaime Ruiz

Here is a plain text file:

As a text file, it has three linesWhat about if we want to

Count words?Change newlines into HTML newlines?Group by words or lines?

Page 15: Lecture 13 - cs.colostate.edu

2/19/17 Slide 152/19/17 Slide 15CSU CT 310 - Web Development ©Ross Beveridge & Jaime Ruiz

Page 16: Lecture 13 - cs.colostate.edu

2/19/17 Slide 16

First Part – Split on Newlines

2/19/17 Slide 16CSU CT 310 - Web Development ©Ross Beveridge & Jaime Ruiz

First, note one command reads file.Now, one command builds an array, one array entry per line.

preg_split well worth studying!

Page 17: Lecture 13 - cs.colostate.edu

2/19/17 Slide 17

Second Part – Word Counts

2/19/17 Slide 17CSU CT 310 - Web Development ©Ross Beveridge & Jaime Ruiz

Ever explode a string?

Similar to preg_split, but simpler, and better if there is only one delimiter.

Pay attention to the ease of word counting.

Page 18: Lecture 13 - cs.colostate.edu

2/19/17 Slide 18

Third Part - Substitution

2/19/17 Slide 18CSU CT 310 - Web Development ©Ross Beveridge & Jaime Ruiz

Explicitly convert ASCII newlines to line break tags.

Note need to avoid counting <br> tags.