32
Anne Rue Regional Account Manager [email protected] (310) 480 4161 Mike Pavlak Solutions Consultant [email protected] (815) 722 3454 How RPG Programmers Can Leverage PHP Arrays The Omni User 7 th Day of Education

How RPG Programmers Can Leverage PHP Arrays Arrays for RPG Programmers.pdf · Why are we talking about arrays? • Fastest method for manipulating ordered sets • Highly leveraged

  • Upload
    dinhbao

  • View
    255

  • Download
    6

Embed Size (px)

Citation preview

Page 1: How RPG Programmers Can Leverage PHP Arrays Arrays for RPG Programmers.pdf · Why are we talking about arrays? • Fastest method for manipulating ordered sets • Highly leveraged

Anne RueRegional Account [email protected](310) 480 4161

Mike PavlakSolutions [email protected](815) 722 3454

How RPG Programmers Can Leverage PHP Arrays

The Omni User7th Day of Education

Page 2: How RPG Programmers Can Leverage PHP Arrays Arrays for RPG Programmers.pdf · Why are we talking about arrays? • Fastest method for manipulating ordered sets • Highly leveraged

Agenda

• Introduce arrays in PHP• Review RPG arrays• Compare RPG and PHP array concepts• More functions for arrays in PHP• Q&A

11/4/2009 | 2

Page 3: How RPG Programmers Can Leverage PHP Arrays Arrays for RPG Programmers.pdf · Why are we talking about arrays? • Fastest method for manipulating ordered sets • Highly leveraged

Why are we talking about arrays?

• Fastest method for manipulating ordered sets • Highly leveraged in PHP development• PHP developers take them for granted• Available in RPG but long neglected• Gap that needs to be closed• Array defined:

…a data structure consisting of a group of elements that are accessed by indexing

11/4/2009 | 3

Page 4: How RPG Programmers Can Leverage PHP Arrays Arrays for RPG Programmers.pdf · Why are we talking about arrays? • Fastest method for manipulating ordered sets • Highly leveraged

PHP Array Examples

Page 5: How RPG Programmers Can Leverage PHP Arrays Arrays for RPG Programmers.pdf · Why are we talking about arrays? • Fastest method for manipulating ordered sets • Highly leveraged

Data Type Review: 8 Data Types

• Scalar• String “the quick brown fox...”, ‘123456’• Integer 860, -9, 57009• Floating point 19.99, 29.99, 3.1412• Boolean true, false

• Compound• Array [0] => 0 [1] => 1 [2] => 1 [3] => 2 [4] => 3…• Object OOP

• Special• Resource Handle• Null Something that not nothing (empty set)

11/4/2009 | 5

Page 6: How RPG Programmers Can Leverage PHP Arrays Arrays for RPG Programmers.pdf · Why are we talking about arrays? • Fastest method for manipulating ordered sets • Highly leveraged

Three types of arrays

• Enumerated• Simple list

• Associative• Custom key

$arrayone = array(“Scooby”, “Shaggy”, “Daphne”, “Fred”, “Velma”);

$arraytwo = array( Cartoon1=>’Scooby’, Cartoon2=>’Shaggy’, Cartoon3=>’Daphne’, y

• Multidimensional• Array of arrays

Cartoon4=>’Fred’, Cartoon5=>‘Velma’ );

$arraythree = array(array(‘Scooby’, ‘Shaggy’, ‘Daphne’,

‘Fred’, ‘Velma’),array(‘Bugs’, ‘Daffy’, ‘Tweety’,

‘Elmer’, ‘Foghorn’) );

11/4/2009 | 6

Page 7: How RPG Programmers Can Leverage PHP Arrays Arrays for RPG Programmers.pdf · Why are we talking about arrays? • Fastest method for manipulating ordered sets • Highly leveraged

Enumerated array

Code:

Output:

Array one: Array ( [0] => Scooby [1] => Shaggy [2] => Daphne [3] => Fred [4] => Velma )

11/4/2009 | 7

Page 8: How RPG Programmers Can Leverage PHP Arrays Arrays for RPG Programmers.pdf · Why are we talking about arrays? • Fastest method for manipulating ordered sets • Highly leveraged

Associative array

Code:

Output: Array two: Array ( [Cartoon1] => Scooby [Cartoon2] => Shaggy [Cartoon3] => Daphne [Cartoon4] => Fred [Cartoon5] => Velma )

If you have trouble, think CL command parameters: Keyword & Values!!!

11/4/2009 | 8

Page 9: How RPG Programmers Can Leverage PHP Arrays Arrays for RPG Programmers.pdf · Why are we talking about arrays? • Fastest method for manipulating ordered sets • Highly leveraged

Multidimensional array

Code:

Output:

Array three: Array ( [0] => Array ( [0] => Scooby [1] => Shaggy [2] => Daphne [3] => Fred [4] => Velma ) [1] => Array ( [0] => Bugs [1] => Daffy [2] => Tweety [3] => Elmer [4] => Foghorn ) )

11/4/2009 | 9

Page 10: How RPG Programmers Can Leverage PHP Arrays Arrays for RPG Programmers.pdf · Why are we talking about arrays? • Fastest method for manipulating ordered sets • Highly leveraged

Adding elements & growing the array

• PHP Arrays are dynamic• Can be sized on the fly, no need to recompile• Example adding element:

11/4/2009 | 10

Page 11: How RPG Programmers Can Leverage PHP Arrays Arrays for RPG Programmers.pdf · Why are we talking about arrays? • Fastest method for manipulating ordered sets • Highly leveraged

Removing elements & reducing the array

• array_pop removes element from the end• unset removes an element you specify (or entire array!)

11/4/2009 | 11

Page 12: How RPG Programmers Can Leverage PHP Arrays Arrays for RPG Programmers.pdf · Why are we talking about arrays? • Fastest method for manipulating ordered sets • Highly leveraged

Trivia points

• Really only one type of array, associative• Data content is non-restrictive, any data types• Each element can be different• Array sizes change dynamically• Supports no known limit of dimensions pp

! Memory ! Humans like 2 or 3 (Think spreadsheet and workbook)

• Used heavily in i/o• Both keys and content can be dynamic• Index starts at zero while RPG starts at one

11/4/2009 | 12

Page 13: How RPG Programmers Can Leverage PHP Arrays Arrays for RPG Programmers.pdf · Why are we talking about arrays? • Fastest method for manipulating ordered sets • Highly leveraged

Got Doc? php.net/array

11/4/2009 | 13

Page 14: How RPG Programmers Can Leverage PHP Arrays Arrays for RPG Programmers.pdf · Why are we talking about arrays? • Fastest method for manipulating ordered sets • Highly leveraged

Review RPG Arrays

Page 15: How RPG Programmers Can Leverage PHP Arrays Arrays for RPG Programmers.pdf · Why are we talking about arrays? • Fastest method for manipulating ordered sets • Highly leveraged

In the beginning…

• Indicators were the only ordered set• Original RPG and RPG II

Name Indicators NotesNumbered *IN01-*IN99 Gen purposeCommand Key *INKA - *INKY No “O”Halt H1-H9 Error recoveryMatching M1-M9, MR Matching recordsControl L1-L9 Level BreaksExternal U1-U8 SwitchesCycle 1P, LR, OA-OG, OV Printing

11/4/2009 | 15

Page 16: How RPG Programmers Can Leverage PHP Arrays Arrays for RPG Programmers.pdf · Why are we talking about arrays? • Fastest method for manipulating ordered sets • Highly leveraged

And then…

• RPG II - Then came simple arrays.• Predefined length• Single variable data type• Built in E-specs

• Op CodesXFOOT Summing aray• XFOOT – Summing aray

• MOVEA – Move data (Still most extremely powerful)• LOKUP – Search the array• SORTA – Gee, I wonder what this does?

• Seems like things paused here for a while

11/4/2009 | 16

Page 17: How RPG Programmers Can Leverage PHP Arrays Arrays for RPG Programmers.pdf · Why are we talking about arrays? • Fastest method for manipulating ordered sets • Highly leveraged

Today…

• Compile time tables• Great for static content• Defined below “O” specs• Two dimensional in nature

• RPG III – Multiple Occurrence Data Structure (MODS)Two dimensional feel• Two dimensional feel

• Still a little clunky

• RPG IV – More Power! • V5R1 – BIF’s : %LOOKUP, %LOOKUPGT, etc.• V5R2 – DIM for Data Structures; MODS on Steroids!• V5R3 – %SUBARR is an attempt at dynamic sizing• V5R4 – XML processing• i6.1 – DIM up to 16,773,104

11/4/2009 | 17

Page 18: How RPG Programmers Can Leverage PHP Arrays Arrays for RPG Programmers.pdf · Why are we talking about arrays? • Fastest method for manipulating ordered sets • Highly leveraged

How PHP matches up to RPG

Page 19: How RPG Programmers Can Leverage PHP Arrays Arrays for RPG Programmers.pdf · Why are we talking about arrays? • Fastest method for manipulating ordered sets • Highly leveraged

Array shootout

• Base functions• RPG has about a dozen op-codes and BIF’s (Variations on BIF’s)• Many op-codes can manipulate array content• PHP has 75 functions www.php.net/array

• SizeRPG has limits 16 773 104 as if i6 1• RPG has limits, 16,773,104 as if i6.1

• PHP has no practical limits, No “array index overflow” error• RPG array must be defined, PHP grows dynamically

• Type• RPG uses static typing (one type, one length)• PHP is dynamically typed (Each element can be different)

11/4/2009 | 19

Page 20: How RPG Programmers Can Leverage PHP Arrays Arrays for RPG Programmers.pdf · Why are we talking about arrays? • Fastest method for manipulating ordered sets • Highly leveraged

Simple Array Search (Lookup)

RPG

PHP

I found her in position==> 2

11/4/2009 | 20

Page 21: How RPG Programmers Can Leverage PHP Arrays Arrays for RPG Programmers.pdf · Why are we talking about arrays? • Fastest method for manipulating ordered sets • Highly leveraged

Simple traverse

RPG

PHP Scooby is the index value 0Shaggy is the index value 1Daphne is the index value 2Fred is the index value 3Velma is the index value 4

11/4/2009 | 21

Page 22: How RPG Programmers Can Leverage PHP Arrays Arrays for RPG Programmers.pdf · Why are we talking about arrays? • Fastest method for manipulating ordered sets • Highly leveraged

RPG to PHP function map

Function RPG PHP NotesSearch %LOOKUP array_searchSum %XFOOT array_sum Array_prod can multiplyGet portion %SUBARR array_slice Substring an array by chunksS t SORTA t t PHP d iSort SORTA asort, arsort PHP sequence dynamicMove MOVEA array_slice Substring by characterCount %ELEM count Get number of elements

11/4/2009 | 22

Page 23: How RPG Programmers Can Leverage PHP Arrays Arrays for RPG Programmers.pdf · Why are we talking about arrays? • Fastest method for manipulating ordered sets • Highly leveraged

More functions in PHP

Page 24: How RPG Programmers Can Leverage PHP Arrays Arrays for RPG Programmers.pdf · Why are we talking about arrays? • Fastest method for manipulating ordered sets • Highly leveraged

Interesting functions

• How to move around the array• Randomize contents• Array housekeeping• Move array elements to variables• Sort two or more arrays at once• Execute a function on each element with no loop!• Data file example

11/4/2009 | 24

Page 25: How RPG Programmers Can Leverage PHP Arrays Arrays for RPG Programmers.pdf · Why are we talking about arrays? • Fastest method for manipulating ordered sets • Highly leveraged

Navigate the array…Thanks Jon!

11/4/2009 | 25

Page 26: How RPG Programmers Can Leverage PHP Arrays Arrays for RPG Programmers.pdf · Why are we talking about arrays? • Fastest method for manipulating ordered sets • Highly leveraged

Mix it up with a shuffle

11/4/2009 | 26

Page 27: How RPG Programmers Can Leverage PHP Arrays Arrays for RPG Programmers.pdf · Why are we talking about arrays? • Fastest method for manipulating ordered sets • Highly leveraged

Consolidate, clean and sort arrays

11/4/2009 | 27

Page 28: How RPG Programmers Can Leverage PHP Arrays Arrays for RPG Programmers.pdf · Why are we talking about arrays? • Fastest method for manipulating ordered sets • Highly leveraged

Sort Multiple Arrays at once!

11/4/2009 | 28

Page 29: How RPG Programmers Can Leverage PHP Arrays Arrays for RPG Programmers.pdf · Why are we talking about arrays? • Fastest method for manipulating ordered sets • Highly leveraged

Manipulate all elements of an array

11/4/2009 | 29

Page 30: How RPG Programmers Can Leverage PHP Arrays Arrays for RPG Programmers.pdf · Why are we talking about arrays? • Fastest method for manipulating ordered sets • Highly leveraged

Get data from a file

• Loop through datap g• List function copies to variables• Implicit copy, be careful• Arrays in PHP like Data

Structures in RPG: The workhorse of data manipulation!

11/4/2009 | 30

Page 31: How RPG Programmers Can Leverage PHP Arrays Arrays for RPG Programmers.pdf · Why are we talking about arrays? • Fastest method for manipulating ordered sets • Highly leveraged

New book, new printing, same great stuff!

Kevin Schroeder from Zend’s Global Services Group

with Jeff Olen, co-author of…

Get yours at MCPressonlineor at fine bookstores

everywhere

Page 32: How RPG Programmers Can Leverage PHP Arrays Arrays for RPG Programmers.pdf · Why are we talking about arrays? • Fastest method for manipulating ordered sets • Highly leveraged

Questions?

Thank you!!

[email protected]

| 4-Nov-09Increasing Maturity of PHP Applications | 32

(310) 480 4161

[email protected]