+ All Categories
Home > Documents > PHP Arrays for RPG Programmers - Gateway/400 · PDF fileWhy are we talking about arrays?...

PHP Arrays for RPG Programmers - Gateway/400 · PDF fileWhy are we talking about arrays?...

Date post: 01-Feb-2018
Category:
Upload: hoangquynh
View: 235 times
Download: 0 times
Share this document with a friend
33
PHP Arrays for RPG Programmers © All rights reserved. Zend Technologies, Inc. Mike Pavlak Solutions Consultant [email protected] (815) 722 3454 Function Junction
Transcript
Page 1: PHP Arrays for RPG Programmers - Gateway/400 · PDF fileWhy are we talking about arrays? • Fastest method for manipulating ordered sets • Highly leveraged in PHP development •

PHP Arrays for RPG Programmers

© All rights reserved. Zend Technologies, Inc.

Mike PavlakSolutions [email protected](815) 722 3454

Function Junction

Page 2: PHP Arrays for RPG Programmers - Gateway/400 · PDF fileWhy are we talking about arrays? • Fastest method for manipulating ordered sets • Highly leveraged in PHP development •

PHP Sessions• PHP101Session 1-9:00

• PHP Arrays for the RPG ProgrammerSession 2-10:30

• Lunch11:45

PHP F ti J tiSession 3 12:30

© All rights reserved. Zend Technologies, Inc.| 2 Copyright © 2009 Zend Technologies, Inc, All rights reserved 02/03/

10

• PHP Function JunctionSession 3-12:30

• PHP Toolkit FunctionsSession 4-1:45

• Build your Intranet on IBM i for FreeSession 5-3:00

• Goodies!4:00

Page 3: PHP Arrays for RPG Programmers - Gateway/400 · PDF fileWhy are we talking about arrays? • Fastest method for manipulating ordered sets • Highly leveraged in PHP development •

Agenda

• Introduce arrays in PHP

• Review RPG arrays

• Compare RPG and PHP array concepts

• More functions for arrays in PHP

© All rights reserved. Zend Technologies, Inc.

More functions for arrays in PHP

• Q&A

| 3 02/04/10

Page 4: PHP Arrays for RPG Programmers - Gateway/400 · PDF fileWhy are we talking about arrays? • Fastest method for manipulating ordered sets • Highly leveraged in PHP development •

Why are we talking about arrays?

• Fastest method for manipulating ordered sets

• Highly leveraged in PHP development

• PHP developers take them for granted

A il bl i RPG b t l l t d

© All rights reserved. Zend Technologies, Inc.

• Available in RPG but long neglected

• Gap that needs to be closed

• Array defined:

| 4 02/04/10

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

Page 5: PHP Arrays for RPG Programmers - Gateway/400 · PDF fileWhy are we talking about arrays? • Fastest method for manipulating ordered sets • Highly leveraged in PHP development •

PHP Array Examples

© All rights reserved. Zend Technologies, Inc.

y p

Page 6: PHP Arrays for RPG Programmers - Gateway/400 · PDF fileWhy are we talking about arrays? • Fastest method for manipulating ordered sets • Highly leveraged in PHP development •

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

© All rights reserved. Zend Technologies, Inc.

• 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)| 6 02/04/

10

Page 7: PHP Arrays for RPG Programmers - Gateway/400 · PDF fileWhy are we talking about arrays? • Fastest method for manipulating ordered sets • Highly leveraged in PHP development •

Three types of arrays

• Enumerated

• Simple list

• Associative

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

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

Cartoon3=>’Daphne’,

© All rights reserved. Zend Technologies, Inc.

• Custom key

• Multidimensional

• Array of arrays

| 7 02/04/10

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

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

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

‘Elmer’, ‘Foghorn’) );

Page 8: PHP Arrays for RPG Programmers - Gateway/400 · PDF fileWhy are we talking about arrays? • Fastest method for manipulating ordered sets • Highly leveraged in PHP development •

Enumerated array

Code:

© All rights reserved. Zend Technologies, Inc.| 8 02/04/10

Output:

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

Page 9: PHP Arrays for RPG Programmers - Gateway/400 · PDF fileWhy are we talking about arrays? • Fastest method for manipulating ordered sets • Highly leveraged in PHP development •

Associative array

Code:

© All rights reserved. Zend Technologies, Inc.| 9 02/04/10

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

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

Page 10: PHP Arrays for RPG Programmers - Gateway/400 · PDF fileWhy are we talking about arrays? • Fastest method for manipulating ordered sets • Highly leveraged in PHP development •

Multidimensional array

Code:

© All rights reserved. Zend Technologies, Inc.| 10 02/04/10

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 ) )

Page 11: PHP Arrays for RPG Programmers - Gateway/400 · PDF fileWhy are we talking about arrays? • Fastest method for manipulating ordered sets • Highly leveraged in PHP development •

Adding elements & growing the array• PHP Arrays are dynamic

• Can be sized on the fly, no need to recompile

• Example adding element:

© All rights reserved. Zend Technologies, Inc.| 11 02/04/10

Page 12: PHP Arrays for RPG Programmers - Gateway/400 · PDF fileWhy are we talking about arrays? • Fastest method for manipulating ordered sets • Highly leveraged in PHP development •

Removing elements & reducing the array• array_pop removes element from the end

• unset removes an element you specify (or entire array!)

© All rights reserved. Zend Technologies, Inc.| 12 02/04/10

Page 13: PHP Arrays for RPG Programmers - Gateway/400 · PDF fileWhy are we talking about arrays? • Fastest method for manipulating ordered sets • Highly leveraged in PHP development •

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

© All rights reserved. Zend Technologies, Inc.

• Supports no known limit of dimensions

! 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

| 13 02/04/10

Page 14: PHP Arrays for RPG Programmers - Gateway/400 · PDF fileWhy are we talking about arrays? • Fastest method for manipulating ordered sets • Highly leveraged in PHP development •

Got Doc? php.net/array

© All rights reserved. Zend Technologies, Inc.| 14 02/04/10

Page 15: PHP Arrays for RPG Programmers - Gateway/400 · PDF fileWhy are we talking about arrays? • Fastest method for manipulating ordered sets • Highly leveraged in PHP development •

Review RPG Arrays

© All rights reserved. Zend Technologies, Inc.

y

Page 16: PHP Arrays for RPG Programmers - Gateway/400 · PDF fileWhy are we talking about arrays? • Fastest method for manipulating ordered sets • Highly leveraged in PHP development •

In the beginning…• Indicators were the only ordered set

• Original RPG and RPG II

Name Indicators Notes

Numbered *IN01-*IN99 Gen purpose

Command Key *INKA - *INKY No “O”

© All rights reserved. Zend Technologies, Inc.| 16 02/04/10

Halt H1-H9 Error recovery

Matching M1-M9, MR Matching records

Control L1-L9 Level Breaks

External U1-U8 Switches

Cycle 1P, LR, OA-OG, OV Printing

Page 17: PHP Arrays for RPG Programmers - Gateway/400 · PDF fileWhy are we talking about arrays? • Fastest method for manipulating ordered sets • Highly leveraged in PHP development •

And then…• RPG II - Then came simple arrays.

• Predefined length

• Single variable data type

• Built in E-specs

• Op Codes

© All rights reserved. Zend Technologies, Inc.

• Op Codes

• 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

| 17 02/04/10

Page 18: PHP Arrays for RPG Programmers - Gateway/400 · PDF fileWhy are we talking about arrays? • Fastest method for manipulating ordered sets • Highly leveraged in PHP development •

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

© All rights reserved. Zend Technologies, Inc.

• 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

| 18 02/04/10

Page 19: PHP Arrays for RPG Programmers - Gateway/400 · PDF fileWhy are we talking about arrays? • Fastest method for manipulating ordered sets • Highly leveraged in PHP development •

How PHP matches up to RPG

© All rights reserved. Zend Technologies, Inc.

Page 20: PHP Arrays for RPG Programmers - Gateway/400 · PDF fileWhy are we talking about arrays? • Fastest method for manipulating ordered sets • Highly leveraged in PHP development •

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

• Size

© All rights reserved. Zend Technologies, Inc.

• Size

• 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)| 20 02/04/

10

Page 21: PHP Arrays for RPG Programmers - Gateway/400 · PDF fileWhy are we talking about arrays? • Fastest method for manipulating ordered sets • Highly leveraged in PHP development •

Simple Array Search (Lookup)

RPG

© All rights reserved. Zend Technologies, Inc.

PHP

| 21 02/04/10

I found her in position==> 2

Page 22: PHP Arrays for RPG Programmers - Gateway/400 · PDF fileWhy are we talking about arrays? • Fastest method for manipulating ordered sets • Highly leveraged in PHP development •

Simple traverse

RPG

© All rights reserved. Zend Technologies, Inc.

PHP

| 22 02/04/10

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

Page 23: PHP Arrays for RPG Programmers - Gateway/400 · PDF fileWhy are we talking about arrays? • Fastest method for manipulating ordered sets • Highly leveraged in PHP development •

RPG to PHP function map

Function RPG PHP Notes

Search %LOOKUP array_search

Sum %XFOOT array_sum Array_prod can multiply

Get portion %SUBARR array_slice Substring an array by chunks

© All rights reserved. Zend Technologies, Inc.| 23 02/04/10

Sort SORTA asort, arsort PHP sequence dynamic

Move MOVEA array_slice Substring by character

Count %ELEM count Get number of elements

Page 24: PHP Arrays for RPG Programmers - Gateway/400 · PDF fileWhy are we talking about arrays? • Fastest method for manipulating ordered sets • Highly leveraged in PHP development •

More functions in PHP

© All rights reserved. Zend Technologies, Inc.

Page 25: PHP Arrays for RPG Programmers - Gateway/400 · PDF fileWhy are we talking about arrays? • Fastest method for manipulating ordered sets • Highly leveraged in PHP development •

Interesting functions

• How to move around the array

• Randomize contents

• Array housekeeping

• Move array elements to variables

© All rights reserved. Zend Technologies, Inc.

Move array elements to variables

• Sort two or more arrays at once

• Execute a function on each element with no loop!

• Data file example

| 25 02/04/10

Page 26: PHP Arrays for RPG Programmers - Gateway/400 · PDF fileWhy are we talking about arrays? • Fastest method for manipulating ordered sets • Highly leveraged in PHP development •

Navigate the array…Thanks Jon!

© All rights reserved. Zend Technologies, Inc.| 26 02/04/10

Page 27: PHP Arrays for RPG Programmers - Gateway/400 · PDF fileWhy are we talking about arrays? • Fastest method for manipulating ordered sets • Highly leveraged in PHP development •

Mix it up with a shuffle

© All rights reserved. Zend Technologies, Inc.| 27 02/04/10

Page 28: PHP Arrays for RPG Programmers - Gateway/400 · PDF fileWhy are we talking about arrays? • Fastest method for manipulating ordered sets • Highly leveraged in PHP development •

Consolidate, clean and sort arrays

© All rights reserved. Zend Technologies, Inc.| 28 02/04/10

Page 29: PHP Arrays for RPG Programmers - Gateway/400 · PDF fileWhy are we talking about arrays? • Fastest method for manipulating ordered sets • Highly leveraged in PHP development •

Sort Multiple Arrays at once!

© All rights reserved. Zend Technologies, Inc.| 29 02/04/10

Page 30: PHP Arrays for RPG Programmers - Gateway/400 · PDF fileWhy are we talking about arrays? • Fastest method for manipulating ordered sets • Highly leveraged in PHP development •

Manipulate all elements of an array

© All rights reserved. Zend Technologies, Inc.| 30 02/04/10

Page 31: PHP Arrays for RPG Programmers - Gateway/400 · PDF fileWhy are we talking about arrays? • Fastest method for manipulating ordered sets • Highly leveraged in PHP development •

Get data from a file

• Loop through data

© All rights reserved. Zend Technologies, Inc.

p g

• List function copies to variables

• Implicit copy, be careful

• Arrays in PHP like Data Structures in RPG: The workhorse of data manipulation!

| 31 02/04/10

Page 32: PHP Arrays for RPG Programmers - Gateway/400 · PDF fileWhy are we talking about arrays? • Fastest method for manipulating ordered sets • Highly leveraged in PHP development •

New book, new printing, same great stuff!

Kevin Schroeder from Zend’s Global Services Group

with

Jeff Olen, co-author of…

© All rights reserved. Zend Technologies, Inc.

Get yours at MCPressonline

or at fine bookstores everywhere

Page 33: PHP Arrays for RPG Programmers - Gateway/400 · PDF fileWhy are we talking about arrays? • Fastest method for manipulating ordered sets • Highly leveraged in PHP development •

Q&Awww.zend.com

[email protected]

© All rights reserved. Zend Technologies, Inc.33Insert->Header & Footer


Recommended