SCOUG-Programming Mailing List Archives
Return to [ 15 |
April |
2005 ]
<< Previous Message <<
Content Type: text/plain
I was going to reply to Lynn's "Programming: Progress--Step 3" post but it took a tangent before I
could respond. So I'm starting a new thread.
The part of Lynn's post that will be the basis of my remarks is:
#######################
Suppose, for example that we wanted to create a
table, an array of character variables, from an input. Suppose
further that we didn't know how many input instances we
had. We can read them from the input, allocate them onto a
stack, maintain a count of the allocations, and on reaching
end of file allocate an array of the correct dimensions. Then
we could pop them from the stack, assigning them to their
relative position in the array.
example: proc options(main);
dcl beta char(40) controlled;
dcl array (*) char(40) controlled;
dcl count fixed bin (31) static init(0);
dcl eof_sw char(1) static init('0');
dcl input file input record;
on endfile (input) begin;
eof_sw = '1';
close file (input);
end;
open file (input);
allocate beta;
read file (input) into (beta);
do while eof_sw = '0';
count += 1;
allocate beta;
read file (input) into (beta);
end;
if count ^= 0
then do;
allocate array (count);
end;
do while (count ^= 0);
array(count) = beta;
free beta;
count -= 1;
end;
#######################
The code snippet above which reads in a variable series of characters of unknown [at compile time]
length into an array is long and wearisome because of the shortcommings (age?) of PL/I. There are 5
declarations and a routine to place a flag at the end of the data file before even getting to actual
data inputting.
Then a datum is brought in one piece at a time, a memory chunk to hold it is allocated, and the
datum ia stored and counted until all data is in a buffer. All of that because PL/I has to be told
what to expect before it accepts it.
Finally, PL/I can be told the actual array size so it can allocate the array memory. With the final
step to transfer the data from the buffer into the array with the programmer being mindful to
release buffer memory. Arrrg.
Just as an exercise I wrote a Python script to input the above snippet into a Python structure that
can act as a directly addressable array -- a dictionary. Note1: the file pl1Example.txt contains
the above code snippet from Lynn's post. Note2: a more fully commented version is far below.
######################
array = {} #create an empty dictionary
f = file('pl1Example.txt','r') #open the data file
Lines = f.readlines() #suck the whole file into a single var (as a list, if want to know)
for i in range(len(Lines)): #put each line in the dictionary
array[i] = Lines[i][0:-1]
f.close()
######################
#print several random lines via indexing
print array[0]
print array[2]
print array[5]
print array[10]
print array[20]
print 'the number of lines is: ', len(array)
The results of these print statements, in respective order, with line notation comments added:
example: proc options(main); #line 1
dcl array (*) char(40) controlled; #line 3
dcl input file input record; #line 6
open file (input); #line 11
allocate array (count); #line 21 - no processing done for leading spaces
the number of lines is: 27
*******************
Sidebar:
I realize my pulling all of the data file into memory [Lines = f.readlines()] is not different than
Lynn using a buffer but I didn't have to allocate/deallocate memory and count pieces of datum.
Furthermore, Python's file module has a 'readline' attribute that can be used in a loop to read in
one line at a time [Line = f.readline()] if the data file is huge. I did it my way because I'm
comfortable working with a list.
*******************
One of my favorite languages in which to program was Ada. But after Python I would not bother to do
so again. Too much grunt work that the compiler should take care of. Python proved that. PL/I is
worse. I would not trouble myself to learn it.
My suggestion is to get off the sticky pad of the merits of PL/I, APL2, and LISP and agree our SL/1
language should have the data types of PL/I, the operations of APL2 (enhanced with the list
aggregate from LISP), but with the ease of use of Python. I don't want to see "declare this,
declare that, allocate this, deallocate that, begin/end, and semi-colons". Let the interpreter and
compiler do all of that nasty work. If Python can do it so can SL/1.
Sheridan
For those that want more complete comments:
######################
#use a dictionary as an array.
# a dictionary is a set of key:value pairs. The key can be anything as long
# as it is immutable and hashable. This means we can simulate an n-dimension
# array with a dictionary using numbers as the key. For example: {(x,y,z):value} where x, y, z
# are numbers will simulate a three-dimension array. BTW, value part can be any Python
# object including another dictionary.
array = {} #create an empty dictionary
#input a file, split it into tokens (separate on c/r), and stuff them into
# the dictionary using a single number as the key (i.e. one-dimension array).
f = file('pl1Example.txt','r') #open the input file
Lines = f.readlines() #suck the whole file into a single var (as a list of lines, if want to know)
#place each line into the dictionary
for i in range(len(Lines)):
array[i] = Lines[i][0:-1] #the end limit of -1 eliminates the 'new line' character
f.close() #close the input file
=====================================================
To unsubscribe from this list, send an email message
to "steward@scoug.com". In the body of the message,
put the command "unsubscribe scoug-programming".
For problems, contact the list owner at
"postmaster@scoug.com".
=====================================================
<< Previous Message <<
Return to [ 15 |
April |
2005 ]
The Southern California OS/2 User Group
P.O. Box 26904
Santa Ana, CA 92799-6904, USA
Copyright 2001 the Southern California OS/2 User Group. ALL RIGHTS
RESERVED.
SCOUG, Warp Expo West, and Warpfest are trademarks of the Southern California OS/2 User Group.
OS/2, Workplace Shell, and IBM are registered trademarks of International
Business Machines Corporation.
All other trademarks remain the property of their respective owners.
|