The PSION Organiser II Homepage
Forum Home Forum Home Help Help Search Search Members Members Login Login Register Register

Welcome, Guest. Please Login or Register.
Why register?
   The Psion Organiser II Forum
   User Forum
   User Post
(Moderator: Mikesan)
   unlimited indexed access to fields (data files)
« Previous topic | Next topic »
Pages: 1  Reply Reply Notify of replies Notify of replies Send Topic Send Topic Print Print
   Author  Topic: unlimited indexed access to fields (data files)  (Read 1038 times)
Boris
Administrator




   
WWW

Posts: 552
unlimited indexed access to fields (data files)
« on: Apr 12th, 2009, 11:50pm »
Quote Quote Modify Modify

This routine allows you to read the Nth field from the current record of any open data file. It is not bound to the 16 fields restriction of OPL.
 
Knowing that fields in data files are separated by tabs (ascii 9), it is easy to save more than 16 fields in a record. Theoretically such a record could even consist of 254 empty fields.  
Using the DISP command (and also from FIND), you will see all fields, even if there are more than 16, but you can't access the extra data!
 
This is where the following routine becomes useful: You pass it a field number along with the logical name, and you get the field data - or chr$(0) if the field doesn't exist.
 
Source code
GetFld$:(lfn$,index%)
 
REM returns the Nth field of the current record
REM parameters: lfn$       logical file name ("A".."D")
REM             index%     field number
REM returns:    - the specified field as string
REM             - ""  if field is empty
REM             - chr$(0) if field does not exist
REM
REM (!) Data may have more than 16 fields (!)
REM
REM Example:
REM
REM    LOCAL line%,tmp$(255)
REM    OPEN "a:main",A,data$  :REM dummy field
REM
REM    line%=1
REM    tmp$=GetFld$:("A",1)
REM    WHILE tmp$<>chr$(0)
REM      ... (process tmp$)
REM      line%=line%+1
REM      tmp$=GetFld$:("A",line%)
REM    ENDWH
 
local cell%,cnt%,pos%,mc%
local rec$(255)
 
REM check parameters
if upper$(lfn$)<"A" or upper$(lfn$)>"D" :raise 226 :endif
 
REM some magic to get to get the whole record
cell%=$2016+2*(asc(upper$(lfn$))-65)
mc%=$1839 :rem xgdx, rts
rec$=usr$(addr(mc%),peekw(cell%))
REM don't change the 3 lines above, havoc may result!
 
REM parse record
while -1
  cnt%=cnt%+1
  pos%=loc(rec$,chr$(9))
  if pos%=0 :pos%=256 :endif
  if cnt%=index%
    return left$(rec$,pos%-1)
  elseif pos%=256
    break
  endif
  rec$=mid$(rec$,pos%+1,255)
endwh
return chr$(0)

 


BTW  
 
To store more than 16 fields in a record:
 
Open the file with a singe string field (OPEN "C:DATA",A,dummy$)
 
Each time you want to add a new record:
  • stuff the field data into a buffer variable, separated by tabs (buf$="field1"+chr$(9)+"field2"+chr$(9)+....)
  • flush the FileCell by using USE A :LAST :NEXT
  • assign the whole buffer to the dummy field (A.dummy$=buf$)
  • Do this only once per record, and do not edit the record afterwards!
  • use APPEND or UPDATE to save the data.
     
  • « Last Edit: May 12th, 2009, 9:47pm by Boris »

    Triholerahduliäöhh gugguh !! (tyrolean joy jodler)
    Rough translation: There's a new build of Psi2Win!!
    Email: boris[at]psion2[dot]org
    Boris
    Administrator




       
    WWW

    Posts: 552
    access to records (data files)
    « Reply #1 on: Apr 22nd, 2009, 11:03pm »
    Quote Quote Modify Modify

    If you want the whole data of the current record, use this simplified version.
     
    The procedure returns the current record of any open file (not just the one selected by USE) as a single string, including all field separator chars (TAB).
     
    GetRec$:(lfn$)
    REM returns the current record as string (including the tabs)
    REM parameter: lfn$       logical file name ("A".."D")
     
    local cell%,mc%
     
    REM check parameters
    if upper$(lfn$)<"A" or upper$(lfn$)>"D" :raise 226 :endif
     
    REM some magic to get to get the whole record
    cell%=$2016+2*(asc(upper$(lfn$))-65)
    mc%=$1839 :rem xgdx, rts
    return usr$(addr(mc%),peekw(cell%))
    REM don't change the 3 lines above, havoc may result!
     

     
    The following example uses GetRec$: to compare two files:
     
    FileComp:(file1$,file2$)
    REM compares two data files
     
    LOCAL c%
    OPEN file1$,A,data$
    OPEN file2$,B,data$
     
    REM compare no. of records
    c%=COUNT
    USE A
    IF c%<>COUNT :GOTO diff:: :ENDIF
     
    REM compare record by record
    WHILE NOT EOF
      IF GetRec$:("A")<>GetRec$:("B")
        GOTO diff::
      ENDIF
      USE B :NEXT
      USE A :NEXT
    ENDWH
    CLS :PRINT "Files are identical!"; :GET
    GOTO bye::
     
    diff::
    CLS :PRINT "Files are different!"; :GET
     
    bye::
    USE A :CLOSE
    USE B :CLOSE

    Note: the example has not been exhaustively tested, excuse me for any glitches  
    « Last Edit: Apr 22nd, 2009, 11:43pm by Boris »

    Triholerahduliäöhh gugguh !! (tyrolean joy jodler)
    Rough translation: There's a new build of Psi2Win!!
    Email: boris[at]psion2[dot]org
    Pages: 1  Reply Reply Notify of replies Notify of replies Send Topic Send Topic Print Print

    « Previous topic | Next topic »

    The Psion Organiser II Forum » Powered by YaBB 1 Gold - SP 1.1!
    YaBB © 2000-2002,
    Xnull. All Rights Reserved.
    top