Hi Larry,
Here is a message I posted to 3000-L on this topic:
regards,
Jeff Vance, CSY
===============================
Date: Wed, 15 Dec 1999 18:23:22 -0700
Subject: Re: Read record from file into a variable.
> Can someone send me an example of reading a record from a flat
> file, into a Variable.
I am assuming you want to know how to do this via the CI. The basic
problem is that the CI's IO redirection opens the file and causes
the record pointer to be set to the beginning. This is done each time the
IO redirdction is encountered by the CI. Thus, if you try to
read from a file in a WHILE loop using the INPUT command, you
will read only the first record each iteration. Below are some ways
to overcome this restriction.
Method 1: using a MSG file.
---------------------------
PROS: simple, easy to maintain, and an easy way to read small files
or make "quick and dirty" scripts.
CONS: slow, especially if the file is long.
Basic idea:
- use a file equation to create a MSG file
- redirect output to this MSG file
- use the INPUT command to read from the MSG file via IO
redirection.
Example:
PARM p1, p2, ...
#comments...
file myfile;MSG
errclear
continue
listf !p1,2 >*myfile
if hpcierr = 0 then
setvar eof FINFO("*myfile","eof")
while setvar(eof,eof-1) > 0 do
input record_var <*myfile
showvar record_var ... etc...
endwhile
endif
reset myfile
deletevar record_var,eof
Method 2: using a flat file.
----------------------------
PROS: most efficient way in the CI to read files.
CONS: less intuitive and more effort to author.
Basic idea:
- create a file via IO redirection
- xeq the same script with the script's input redirected to the
file created in step 1.
- use the INPUT command or input() function to read from the
file (with no IO redirection since the input for the entire
script is redirected here).
Example:
PARM p1, p2, ..., entry=main
#comments...
if "!entry" = "main" then
# in the main part of the script
errclear
continue
listf !p1,2 >myfile
if hpcierr = 0 then
# execute same script with input redirected
xeq !HPFILE !p1, !p2, ... entry=alternate <myfile
endif
deletevar record_var,eof
return
elseif "!entry" = "alternate" then
# in the 'alternate' script entry point
setvar eof FINFO(HPSTDIN,"eof")
while setvar(eof,eof-1) > 0 do
setvar record_var input()
showvar record_var ... etc..
endwhile
return
endif
Method 3: using PRINT to get an individual record.
--------------------------------------------------
PROS: I've seen this approach used many times so it must be
intuitive.
CONS: inefficient, ok for small files.
Basic idea:
- create a file via IO redirection.
- use the PRINT;start=;end= to create another file with 1 record.
- use the INPUT command to read from the one record file via IO
redirection.
Example:
PARM p1, p2, ...
#comments...
errclear
continue
listf !p1,2 >myfile
if hpcierr = 0 then
setvar eof FINFO("myfile","eof")
setvar i 0
while setvar(i,i+1) <= eof do
print myfile;start=!i;end=!i >myfile2
input record_var <myfile2
showvar record_var ... etc...
endwhile
endif
deletevar record_var,eof,i
---------------------------------------------------------------------------
|