HP Basic Reverse file read

Everything about buying, using, and managing OpenVMS systems not covered by other sections.
Post Reply

Topic author
kwfeese
Member
Posts: 7
Joined: Fri Nov 18, 2022 11:34 am
Reputation: 0
Status: Offline

HP Basic Reverse file read

Post by kwfeese » Tue Feb 28, 2023 6:32 pm

I'm trying to read a file in reverse using HP Basic and I came across this paragraph in Guide to OpenVMS File Applications:

8.4.3 Processing Indexed Files
Indexed files provide the most record-processing flexibility. Your program can read existing records from the file in sequential, random access by RFA mode or random access by key mode. RMS also allows you to write any number of new records into an indexed file if you do not violate a specified key constraint, such as not allowing duplicate key values.

In random access by key mode, RMS provides two forward search key options for use with one of four match options (see Section 8.4.3.2). A reverse search key option permits reverse random access when used in combination with either of the two forward search key options.

There was an example but it was written in C and, unfortunately, my current company doesn't have a C license.

Has anyone ever accomplished this and if so can you provide examples?


sms
Master
Posts: 114
Joined: Fri Aug 21, 2020 5:18 pm
Reputation: 0
Status: Offline

Re: HP Basic Reverse file read

Post by sms » Tue Feb 28, 2023 8:32 pm

Code: Select all

> I'm trying to read a file in reverse [...]

   Is this an existing file (with some kind of structure), or some new
file which you'll be creating in some way or other, or what?

   It might be helpful to describe in more detail the actual problem
which you are trying to solve, rather than asking how to implement some
particular "solution" or technique ("read a file in reverse").

> [...] in Guide to OpenVMS File Applications: [...]
> There was an example but it was written in C [...]

   Have you looked at the BASIC documentation?

      https://docs.vmssoftware.com/   (Look for "COMPILERS"?)

   The "VSI BASIC for OpenVMS User Manual" includes "Chapter 13. File
Input and Output", which includes section 13.2.4 on "Indexed Files".

User avatar

arne_v
Valued Contributor
Posts: 93
Joined: Fri Apr 17, 2020 7:31 pm
Reputation: 0
Location: Rhode Island, USA
Status: Offline
Contact:

Re: HP Basic Reverse file read

Post by arne_v » Tue Feb 28, 2023 8:32 pm

You have seen some C code that are working with an index-sequential file using RMS API specifically SYS$FIND with rab.rab$l_rop set to something | RAB$M_REV and you want to do the same in Basic?

Added in 10 minutes 42 seconds:
In Basic you would most likely not use RMS directly but instead use Basic builtin statements. Instead of SYS$FIND and SYS$GET just use FIND and GET statements.

I am not a Basic person, but I believe that the way to read reverse is to specify PRIMARY KEY ... DESCENDING in the OPEN statement.

https://docs.vmssoftware.com/vsi-basic- ... nce-manual
https://docs.vmssoftware.com/vsi-basic- ... ser-manual

has all the details.

If you get stuck then I could try create a working example.
Arne
arne@vajhoej.dk
VMS user since 1986


fim
Active Contributor
Posts: 30
Joined: Wed Jan 04, 2023 5:14 am
Reputation: 0
Status: Offline

Re: HP Basic Reverse file read

Post by fim » Wed Mar 01, 2023 10:57 am

From what I can see in Basic manuals you can read indexed file only forward. Then there is an option, read the entire file from start to finish in key order, store the keys in an array in memory. Then you retrieve the keys in your array in backward order, with each key you use the basic command FIND with EQ and then the command GET.
/Fim W.


Topic author
kwfeese
Member
Posts: 7
Joined: Fri Nov 18, 2022 11:34 am
Reputation: 0
Status: Offline

Re: HP Basic Reverse file read

Post by kwfeese » Wed Mar 01, 2023 12:59 pm

sms wrote:
Tue Feb 28, 2023 8:32 pm

Code: Select all

> I'm trying to read a file in reverse [...]

   Is this an existing file (with some kind of structure), or some new
file which you'll be creating in some way or other, or what?

   It might be helpful to describe in more detail the actual problem
which you are trying to solve, rather than asking how to implement some
particular "solution" or technique ("read a file in reverse").

> [...] in Guide to OpenVMS File Applications: [...]
> There was an example but it was written in C [...]

   Have you looked at the BASIC documentation?

      https://docs.vmssoftware.com/   (Look for "COMPILERS"?)

   The "VSI BASIC for OpenVMS User Manual" includes "Chapter 13. File
Input and Output", which includes section 13.2.4 on "Indexed Files".
It's an indexed History file currently at 67m blocks and the file is corrupt so I'm trying to extract as much data from the file as possible by sequentially reading the file forwards and backwards. I've tried to use the convert utility to no avail.


sms
Master
Posts: 114
Joined: Fri Aug 21, 2020 5:18 pm
Reputation: 0
Status: Offline

Re: HP Basic Reverse file read

Post by sms » Wed Mar 01, 2023 2:07 pm

Code: Select all

> [...] I've tried to use the convert utility to no avail.

   "tried" is not a very detailed description of what you did, and "to
no avail" is no better as a description of what happened when you did
it.

> [...] read the entire file from start to finish in key order, store
> the keys in an array in memory [...]

   I know nothing about that array stuff, but it did leap out at me as I
was skimming the BASIC docs.

User avatar

arne_v
Valued Contributor
Posts: 93
Joined: Fri Apr 17, 2020 7:31 pm
Reputation: 0
Location: Rhode Island, USA
Status: Offline
Contact:

Re: HP Basic Reverse file read

Post by arne_v » Wed Mar 01, 2023 8:11 pm

Unfortunately primary key descending is for the actual index in the file and can not be changed as one want.

So options are:
- use RMS API (yuck)
- read all keys into array and process in reverse order (sounds like that will not work based on the description)
- read chunks where each chunk is read forward but the order of which chunks in the file is read is reversed (hack)

Illustration of the last:

Code: Select all

$ type gen.pas
program gen(input,output);

type
   fixstring = packed array [1..8] of char;
   mydata = record
               nam : [KEY(0)] fixstring;
               v   : integer;
            end;

var
   f : file of mydata;
   d : mydata;

begin
   open(f, 'fwdrev.isq', new, organization := indexed, access_method := keyed);
   rewrite(f);
   d.nam := 'A A';
   d.v   := 1;
   f^ := d;
   put(f);
   d.nam := 'B B';
   d.v   := 2;
   f^ := d;
   put(f);
   d.nam := 'C C';
   d.v   := 3;
   f^ := d;
   put(f);
   d.nam := 'D D';
   d.v   := 4;
   f^ := d;
   put(f);
   close(f);
end.
$ pas gen
$ link gen
$ run gen
$ type fwd.bas
program fwd

record mydata
    string nam = 8
    integer v
end record
map (buf) mydata d

open "fwdrev.isq" as file #1, indexed fixed, recordtype none, map buf, primary key d::nam
handler eof_handler
end handler
when error use eof_handler
    while 1 = 1
        get #1
        print d::nam,d::v
    next
end when
close #1

end program
$ bas fwd
$ link fwd
$ run fwd
A A            1
B B            2
C C            3
D D            4
$ type rev.bas
program rev

record mydata
    string nam = 8
    integer v
end record
map (buf) mydata d
declare string target
declare integer i

handler eof_handler
end handler

open "fwdrev.isq" as file #1, indexed fixed, recordtype none, map buf, primary key d::nam
for i = 1 to 26
    target = chr$(91 - i)
    when error use eof_handler
        find #1, key #0 ge target
        get #1
        while mid(d::nam, 1, 1) = target
            print d::nam,d::v
            get #1
        next
    end when
next i
close #1

end program
$ bas rev
$ link rev
$ run rev
D D            4
C C            3
B B            2
A A            1
Arne
arne@vajhoej.dk
VMS user since 1986

Post Reply