Pages

Thursday, April 15, 2021

Return Number of Parameters (%PARMS) and Parameter Number (%PARMNUM) - IBM i

Return Number of Parameters (%PARMS)

Built-In Function %PARMS returns the number of parameters passed to the procedure where %PARMS is run. 

This BIF is useful when the procedure contains multiple optional parameters and if the procedure is called from different places with different number of parameters to identify the number of parameters passed. 

This is most commonly used to to handle the procedure logic based on the parameters passed only. 

Value returned by %PARMS is same as *PARMS in Program Status Data Structure if used in the main procedure. 

Let's have a look at the simple example to see how this works. 

%PARMS - RPGLE

In the above example, 
  • Lines - 5 to 9: Prototype declaration of the Sub procedure with one mandatory parameter, two optional parameters and one return value. We will see the code of procedure in a bit. 
  • Line - 18, 22 & 26: Calling the sub procedure with one mandatory parameter, two parameters and three parameters respectively. 
Let's have a look at the code for Sub procedure and see how the %PARMS work. 

%PARMS - RPGLE

In the above procedure, 
  • Lines 42 & 44: We are using %PARMS to determine the number of parameters passed. 
This is often used to handle the parameters (like initializing or making sure these aren't used in the logic etc) that are not passed and make sure the procedure works as expected. 

One thing to note here here, If the RTNPARM keyword is used return parameter would be considered as one of the (first) parameter and total number of parameters would be increased by 1. 

In the above example, If only one parameter is passed, %Parms would be 2 (by considering the return parameter). 

%PARMS with RTNPARM- RPGLE

In the above example, 
  • Line - 34: We are using RTNPARM keyword against the Procedure Interface. The same would need to mentioned in the Prototype as well. 
  • Lines - 42, 44 & 46: We are using %PARMS to check the number of parameters by considering the return parameter. 
    • %PARMS would be 2 if only one parameter is passed.
    • %PARMS would be 3 if two parameters are passed. 
    • %PARMS would be 4 if three parameters are passed. 

Return Parameter Number (%PARMNUM)

Built-In Function %PARMNUM returns the number of the parameter passed from the parameters list. 

Operand for this BIF should always be the parameter specified in the procedure interface. 

Below are the few important points to remember. 
  • %PARMNUM can only be used with the parameters defined in Procedure Interface (PI) and Parameter defined using PLIST cannot be used.
  • Parameter Name must be specified as it is defined in the PI. 
    • If the parameter is an array, Array should be used and not an Index. 
    • If the parameter is a data structure, Data structure should be used and not subfields.
    • If the parameter is a file, File should be used and not record format. 
  • If RTNPARM is used, Return value is considered as the first parameter and the other parameters would be considered next.
Let's have a look at an example without using RTNPARM first to understand how this works. 

PARMNUM - RPGLE

In the above example, 
  • Line - 34: Procedure Interface is defined without RTNPARM. 
  • Lines - 43, 46 & 49: %PARMNUM is used to get the parameter number passed. 
    • Only Parameter names from the Procedure Interface are to be used. 
  • Line - 52: %PARMS is used to return the total number of parameters passed. 
Similar to %PARMS, If we use RTNPARM total number of parameters would be increased by 1 and parameter number also would be increased by 1 as the return value is considered as first parameter. 

Let's add RTMPARM to the above procedure. 

PARMNUM with RTNPARM - RPGLE

By adding RTNPARM to the Procedure Interface, Number of the parameter and total number of parameters passed are increased by 1. 


If you have any Suggestions or Feedback, Please leave a comment below or use Contact Form. 

Thursday, April 8, 2021

String method zfill() in Python

String Method - zfill()

While working with numeric values in a string format, it becomes essential to have the data for a specific length as required i.e., filling the leading blanks with zeros. 

E.g.: If we have number 470 and need to mention this in 5 digits in character format, two leading zeros to be added to make it 5 digits. 

470 --> 00470

String method 'zfill()' is very useful to achieve this. 

Syntax:

str.zfill(width)

Method zfill accepts one argument 'width' which defines the minimum number of digits (width) the return string should be. This method doesn't amend the original string. 

Before we see an example, Below are the few points to consider. 
  • zfill is a string method and cannot be directly used with int or float or other data types. 
  • Width passed as an argument is the minimum number of digits is by considering the period (.) and any prefix (+/-). 
  • If the original string has more width than the width passed in the argument, return string would be same as original string. 
  • No data validation would be done on the string. If the string contains alpha or alphanumeric characters, leading zeros would still be added based on the width passed. 
Let's have a look at few examples to understand this better. 

E.g.: 

Convert the number 470 to a string of 5 digits by appending leading zeros. 

String method zfill() in Python

In the above example, we are doing this in two different ways. 
  • First Method (Line - 2): We are directly mentioning 470 as a string and using the zfill method by passing the width '5' as an argument. This looks easy to understand how zfill works, but we may not be able to do this always and the data tend to change.
  • Second Method (Lines - 6 & 7): We are creating an integer variable by assigning the value 470. And, converting the integer to string by using str() function and using the zfill method on the result. 

The above example is straight forward there are no decimal values or prefixes. So, what would happen if we have decimal values and/or prefixes. 

String method zfill() in Python

In the above example, 
  • Lines - 1 & 2: We are using zfill on the number -470 (prefixed with -, total of 4 digits currently) and the result is '-0470' (only one leading zero is added). Width of the result string is by considering the prefix. 
  • Lines - 5 & 6: We are using zfill on the number -47.0 (prefixed with - and contains ., total of 5 digits currently) and the result is '-47.0' (no change). Current length of the number (converted to string) is same as the width passed, So no change would be done.
Let's have a look at another example where the input number is greater than the width passed. 

String method zfill() in Python

In the above example, both numbers are exceeding the width passed (5) and original string would be returned in this case. 

As mentioned above, No validation would be done on the input string. If alpha or alphanumeric data is passed, zfill would still return a string by appending leading zeros. 

String method zfill() in Python

In the above example, two leading zeros have been added to 'ABC' to return the string of length '5'. 

Hope the above info was useful in understanding the usage of method zfill(). 


If you have any Suggestions or Feedback, Please leave a comment below or use Contact Form.

Wednesday, April 7, 2021

Retrieve list of Open files for a Job from SQL - IBM i

Retrieve List of Open Files

The list of open files for a job can be seen by using the command WRKJOB (Work with Job) or DSPJOB (Display Job) and then by taking option '14'. Or, by using the API 'QDMLOPNF' (List Open Files). 

One other easier way to do this is from SQL by using the table function OPEN_FILES. 

This function accepts a single parameter 'Job Name'.

Job Name (JOB_NAME) - Name of the job (qualified) for which the list of open files are to be retrieved. If current job, '*' can be passed instead of the job name. 

One thing to note here is 'Job Control (*JOBCTL)' authority is required to retrieve the list of files opened by other user's jobs. And, no special authority is required to retrieve our own jobs. 

One advantage of having SQL table function to retrieve the the list of open files based on the specific condition as required. 

E.g.: 
  • Retrieve the list of open physical files.        
Retrieve list of open files from SQL - IBM i
  • Retrieve the list of files opened in OUTPUT mode. 
Retrieve list of open files from SQL - IBM i
  • Retrieve the current RRN number of a specific file opened. 
Retrieve list of open files from SQL - IBM i

and much more as required. 

Below are the some of the key columns present in this table function. 

Library Name (LIBRARY_NAME) - Name of the library that contains the open file.


File Name (FILE_NAME) - Name of the open file.


File Type (FILE_TYPE) - Type of the open file. Below are the valid file types.

  • BSCF - Binary Synchronous Communications (BSC) file
  • CMNF - Communications file
  • DDMF - Distributed Data Management file
  • DKTF - Diskette file (spooled and non-spooled)
  • DSPF - Display file
  • ICFF - Intersystem Communications Function file
  • LF - Logical file
  • MXDF - Mixed file
  • PF - Physical file 
  • PRTF - Printer file (spooled and non-spooled)
  • SAVF - Save file
  • TAPF - Tape file
  • *INLINE - Inline data file

Member Name (MEMBER_NAME) - Name of the database member, If FILE_TYPE is physical (PF) or logical (LF). If multiple member processing is being performed, the value is *ALL.


Record Format (RECORD_FORMAT) - Name of the last record format that was used for an I/O operation to the file.


Activation Group Name (ACTIVATION_GROUP_NAME) - Name of the activation group to which the open file is scoped.

  • *DFTACTGRP - The file is scoped to the default activation group.
  • *NEW - The file is scoped to a *NEW activation group.
  • Contains the null value for a file scoped to the job, not a specific activation group.


Thread ID (THREAD_ID) - Thread handle assigned by the system which identifies the thread in which the file was opened.


Open Option (OPEN_OPTION) - Type of open operation that was performed.

  • ALL - The file was opened for all operations (input, output, update, and delete).
  • INPUT - The file was opened for input operations only.
  • OUTPUT - The file was opened for output operations only.


RRN (RELATIVE_RECORD_NUMBER) - Relative record number of the last record referred to by an I/O or open operation for database files.


For full list of columns present in this table have a look at this link


*This function is only available since IBM i 7.3 TR9 and IBM i 7.4 TR3. 

 
If you have any Suggestions or Feedback, Please leave a comment below or use Contact Form. 

Sunday, April 4, 2021

Usage of map() function in Python

Usage of map() function in Python - Explained

Python map() function is very helpful when we need to call a specific function for each element in an iterable and return the result as an iterable of type map object. 

In other words, map() function would call another function for each element in an iterable (range, list, tuple...) and returns an iterable.

Syntax

map(function, iterable1, iterable2,...)

map() accepts a minimum of two parameters.
  • A function (to be called)
  • An iterable (function to be executed for each element in an iterable)
And, can have multiple iterables as required by the function passed in the first parameter. If the function requires two parameters then two iterables to be passed in parameters for map function. 

Let's have a look at an example to calculate the square of each number in the range of 0 to 5.

Without using map()


map() function in Python

In the above example, 
  • Line - 3: Create empty list to hold square of numbers. 
  • Line - 6: for loop over range of 0 and 5. 
  • Line - 7: pass number to the function 'square' to get the square of the number and add it to the list created. 
  • Line - 13: Function 'square' to accept a number as parameter and return the square of the number as result. 
Here we are using for loop to call the function for each number in the range of 0 and 5. 

We can use map() function to do that instead of having for loop. 

With map() function


map() function in Python

In the above example, 
  • Line - 3: We are passing the function 'square' as first parameter and range(5) as second parameter to the map function and assigning the result to variable 'square_list'. 
Only difference here would be 'square_list' is an iterable of type map object not a list. Print statement above doesn't actually print squares instead it just prints '<map object at memory_location>'.

Values in the map object can either be accessed by using this in for loop or simply by converting the map object to list. 

square_list = list(map(square, range(5)))

Just like in the above example, if we are only creating the function to use it in map, we can avoid function creation by using lambda expression. 

map() with lambda expression


map() with lambda expression in Python

In the above example, 
  • Line - 3: We are using lambda expression to calculate the square of a number in the range 0 to 5 instead of having to create a function. 

map() with two or more iterables

We can pass multiple iterables to map function as required by the function passed. 

Let's have a look at an example with a function to add two numbers and return the result by passing two iterables. 

map function with multiple iterables

In the above example, 
  • Line - 14: Creating a function to accept two numbers and return the sum of two numbers.
  • Lines - 3 & 6: Creating two lists with numbers. Note that the number of elements in the two lists aren't same. 
  • Line - 9: passing the function and two lists to the map function. 
There are couple points to note regarding the way map function works when two or more iterables are passed. 
  • Elements in the same index position would be passed as parameters to the function. E.g.: Index 0 from first list & second list, Index 1 from first list & second list, Index 2 from first list & second list and so on.
  • If the number of elements in the lists aren't same, map function would return the results equal to the smallest index. In the above example second element has got only 3 elements and the result would contain the sum of first 3 elements from the two lists. 

map() function with Python BIF

Like user created functions, we can use Python Built-In Functions as well inside the map() function. 

We will have a look at converting the numbers in a string (or user input) to a list by converting them to int. 

map() with int in Python

In the above example, 
  • Line - 2: A string called 'numbers' with a string containing numbers. This can either be an user input (which is most common scenario) that is to be split into a list of numbers. 
  • Line - 5: We are passing function 'int' and an iterable (by splitting the string into a list using split method). 
Like any other function, int would convert each of the number from the iterable passed and returns the integer value. This is them stored as a map object and converted to list (by using list function). 


Hope the above info was useful in understanding the usage of map() function. 


If you have any Suggestions or Feedback, Please leave a comment below or use Contact Form.

OOPs.. Is Java a pure Object Oriented Language?

Object Oriented Programming Object Oriented Programming or OOPs is a programming model which is based on the Objects.  We probably have to t...