Php arrays for RPG programmers

Post on 08-May-2015

3,223 views 4 download

description

COMMON Europe Congress 2012 - Vienna

transcript

PHP Arrays for RPG Programmers

Function Junction

PHP Arrays for RPG Programmers

Mike PavlakSolutions Consultantmike p@zend com

Function Junction

mike.p@zend.com(815) 722 3454

© All rights reserved. Zend Technologies, Inc.

PHP Sessions PHP Sessions

• What’s New with Zend ServerSun 11:30 AM

• Business Value of PHPSun 1:30 PM

• Practical PHP by Example (Leth-Kjaer)Sun 4:00 PM

• PHP on IBM i: Getting StartedMon 10:00 AM • PHP on IBM i: Getting StartedMon 10:00 AM

• DB Standards in Zend PHP usage (Sielhorst)Mon 10:00 AM

• MySQL on IBM i, Open Source & DB2 Store Tue 10:00 AM

11 30 A© All rights reserved. Zend Technologies, Inc.| 2 Copyright © 2009 Zend Technologies, Inc, All rights

reserved 02/03/10

• PHP Arrays for the RPG ProgrammerTue 11:30 AM

Agenda Agenda

• Introduce arrays in PHPy

• Review RPG arrays

• Compare RPG and PHP array conceptsCompare RPG and PHP array concepts

• More functions for arrays in PHP

• Q&A• Q&A

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

Why are we talking about arrays?Why are we talking about arrays?

• Fastest method for manipulating ordered sets p g

• Highly leveraged in PHP development

• PHP developers take them for granted• 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…a data structure consisting of a group of

elements that are accessed by indexing

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

Introducing PHP Arrays

© All rights reserved. Zend Technologies, Inc.

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

© All rights reserved. Zend Technologies, Inc.

• Resource Handle

• Null Something that not nothing (empty set)| 6 02/04/

10

Three types of arrays (PHP 5.3 notation)

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

• Simple listFred , Velma );

$arraytwo = array( ‘Cartoon1’=>’Scooby’, ‘C t 2’ ’Sh ’• Associative

• Custom key

‘Cartoon2’=>’Shaggy’, ‘Cartoon3’=>’Daphne’, ‘Cartoon4’=>’Fred’, ‘Cartoon5’=>‘Velma’ );

$arraythree = array(

• Multidimensional

array(‘Scooby’, ‘Shaggy’, ‘Daphne’, ‘Fred’, ‘Velma’),

array(‘Bugs’, ‘Daffy’, ‘Tweety’, ‘Elmer’, ‘Foghorn’) );

© All rights reserved. Zend Technologies, Inc.

• Array of arrays

| 7 02/04/10

g ) )

Three types of arrays (PHP 5.4 notation)

• Enumerated $arrayone = [“Scooby”, “Shaggy”, “Daphne”, “Fred” “Velma”];

• Simple listFred , Velma ];

$arraytwo = [ ‘Cartoon1’=>’Scooby’, ‘C t 2’ ’Sh ’• Associative

• Custom key

‘Cartoon2’=>’Shaggy’, ‘Cartoon3’=>’Daphne’, ‘Cartoon4’=>’Fred’, ‘Cartoon5’=>‘Velma’ ];

$arraythree = array[

• Multidimensional

[‘Scooby’, ‘Shaggy’, ‘Daphne’, ‘Fred’, ‘Velma’],

[‘Bugs’, ‘Daffy’, ‘Tweety’, ‘Elmer’, ‘Foghorn’] ];

© All rights reserved. Zend Technologies, Inc.

• Array of arrays

| 8 02/04/10

g ] ]

Enumerated array

Code:

Output:Output:

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

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

Associative array

Code:

Output:

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

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

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

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

Adding elements & growing the arrayAdding elements & growing the array• PHP Arrays are dynamic

C b i d th fl d t il• Can be sized on the fly, no need to recompile

• Example adding element:

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

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

t l t if ( ti !)• unset removes an element you specify (or entire array!)

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

Trivia pointsTrivia points• Really only one type of array…associative

D t t t i t i ti d t t• Data content is non-restrictive, any data types

• Each element can be different

• Array sizes change dynamically

• Supports no known limit of dimensions

How much memory is on your machine?

Humans like 2 or 3 (Think spreadsheet and workbook)

• Used heavily in i/o

• Both index and content can change!

© All rights reserved. Zend Technologies, Inc.

• Index starts at zero while RPG starts at one

| 14 02/04/10

Got Doc? php net/arrayGot Doc? php.net/array

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

Review RPG Arrays

© All rights reserved. Zend Technologies, Inc.

In the beginningIn the beginning…• Indicators were the only ordered set

Original RPG and RPG II• Original RPG and RPG II

Name Indicators Notes

Numbered *IN01-*IN99 Gen purposeNumbered IN01- IN99 Gen purpose

Command Key *INKA - *INKY No “O”

Halt H1-H9 Error recovery

Matching M1-M9, MR Matching records

Control L1-L9 Level Breaks

External U1-U8 SwitchesExternal U1 U8 Switches

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

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

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

Predefined length• Predefined length

• Single variable data type

• Built in E specs• Built in E-specs

• Op Codes

• XFOOT Summing aray• XFOOT – Summing aray

• MOVEA – Move data (Still most extremely powerful)

• LOKUP – Search the array• LOKUP Search the array

• SORTA – Gee, I wonder what this does?

• Seems like things paused here for a while

© All rights reserved. Zend Technologies, Inc.

• Seems like things paused here for a while

| 18 02/04/10

TodayToday…• Compile time tables

• Great for static content

• Defined below “O” specs

• Two dimensional in nature

• RPG III Multiple Occurrence Data Structure (MODS)• RPG III – Multiple Occurrence Data Structure (MODS)

• 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!• V5R2 – DIM for Data Structures; MODS on Steroids!

• V5R3 – %SUBARR is an attempt at dynamic sizing

• V5R4 – XML processing

© All rights reserved. Zend Technologies, Inc.

• i6.1 – DIM up to 16,773,104

• i7.1 – Sort subfields, Ascend-Descend, (still fixed size )| 19 02/04/

10

From the i7 1 manualFrom the i7.1 manual• The first array entry for each record must begin in position 1.

• PHP starts with zero

• All elements must be the same length and follow each other with no intervening spaces

You must be kidding Still?• You must be kidding…Still?

• If the number of elements in the array as specified on the definition specification is greater than the number of entries provided, the remaining elements are filled with the default values for the data type specifiedelements are filled with the default values for the data type specified.

• If you don't know the number of elements you will need in an array until runtime, you can define the array with the maximum size, and then use a subset of the array in your programsubset of the array in your program.

• PHP is far more dynamic and this message leaves with work-files, still…

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

How PHP matches up to RPG

© All rights reserved. Zend Technologies, Inc.

Array shootoutArray shootout• Base functions

RPG has about a dozen op codes and BIF’s (Variations on BIF’s)• 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• PHP has 75 functions www.php.net/array

• Size

• RPG has limits 16 773 104 as if i7 1 (elements & bytes) • RPG has limits, 16,773,104 as if i7.1 (elements & bytes)

• PHP has no practical limits, No “array index overflow” error

• RPG array must be defined PHP grows dynamically (CT <= 100)• RPG array must be defined, PHP grows dynamically (CT <= 100)

• Type

• RPG uses static typing (one type one length)

© All rights reserved. Zend Technologies, Inc.

• RPG uses static typing (one type, one length)

• PHP is dynamically typed (Each element can be different)| 22 02/04/

10

Simple Array Search (Lookup)Simple Array Search (Lookup)

RPG

PHP I found her in position==> 2

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

I found her in position==> 2

Simple traverseSimple traverse

RPG

PHP

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

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

Fred is the index value 3Velma is the index value 4

RPG to PHP function mapRPG to PHP function map

F ti RPG PHP N tFunction RPG PHP Notes

Search %LOOKUP array_search

Sum %XFOOT array sum Array prod can multiplyy_ y_p p y

Get portion %SUBARR array_slice Substring an array by chunks

Sort SORTA asort, arsort PHP sequence dynamic

M MOVEA li S b t i b h tMove MOVEA array_slice Substring by character

Count %ELEM count Get number of elements

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

More functions in PHP

© All rights reserved. Zend Technologies, Inc.

Interesting functionsInteresting functions

• How to move around the arrayy

• Randomize contents

• Array housekeepingArray housekeeping

• Move array elements to variables

• Sort two or more arrays at once• Sort two or more arrays at once

• Execute a function on each element with no loop!

• Data file example• Data file example

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

Navigate the array Thanks Jon!Navigate the array…Thanks Jon!

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

Mix it up with a shuffleMix it up with a shuffle

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

Consolidate clean and sort arraysConsolidate, clean and sort arrays

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

Sort Multiple Arrays at once!Sort Multiple Arrays at once!

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

Manipulate all elements of an arrayManipulate all elements of an array

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

Get data from a fileGet data from a file

• Loop through data

• List function copies to variablesp

• Implicit copy, be careful

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

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

Debugging Arrays

© All rights reserved. Zend Technologies, Inc.

Display the array formattedDisplay the array formatted…• <pre> presents text in fixed format font

Lik i f b t i• Like courier-new for green bar reports conversions

• Used inside many HTML elements

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

Code for debugCode for debug…

© All rights reserved. Zend Technologies, Inc.

New book new printing same great stuff!New book, new printing, same great stuff!

Kevin Schroeder from Zend’s Global Services GroupGlobal Services Group

with

J ff Ol th fJeff Olen, co-author of…

Get yours at MCPressonline

or at fine bookstores everywhere

© All rights reserved. Zend Technologies, Inc.

Join us at ZendConThe premier PHP conference!

October 22-25, 2012 – Santa Clara, CA

Conference ThemesPHP in 2012 - The latest PHP technologies and toolsLearn how to leverage the latest mobile, HTML 5, testing and PHP best practices

Conference Highlights• Sessions focused on how to best develop and deploy PHP

• Sessions designed for all knowledge levelsPHP best practices

Zend Framework 2 - Hit the ground runningLearn how to build faster, more modular and more expandable applications

Development & The Cloud – A love storyLearn how the latest developments in cloud-based services

• Intensive tutorials for accelerated learning

• PHP Certification crash courses and testing

• Exhibit hall showcasing the latest products

• Special networking opportunities during meals and events

© All rights reserved. Zend Technologies, Inc.www.zendcon.com

Learn how the latest developments in cloud-based services, infrastructure and best practices can benefit you

• Special networking opportunities during meals and events

Q&AQ&Awww zend comwww.zend.com

mike p@zend commike.p@zend.com

Please fill out yourPlease fill out your Session Evaluation!

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

Session Evaluation!