PxPlus User Forum

Twitter Twitter Twitter

Author Topic: Read Data to a memory File or just use a switch?  (Read 807 times)

Peter.Higgins

  • Diamond Member
  • *****
  • Posts: 124
    • View Profile
Read Data to a memory File or just use a switch?
« on: January 20, 2022, 02:33:11 PM »
Curious which is the best way to tackle the need to convert a EDI SAC code into one of 4 classification codes like frt or tax in an object.  This could be used 1 times to 2000 times per instantiation with the high occurring during peak load periods. 

I know Read Data with DATA lines is relatively "Old fashioned" but given the Standards in play and limited maintenance usage of the object it makes a lot of sense where as a maintenance file with FMs does not.

So considering DATA lines on instantiation writing to a memory file for a method, or a method with a switch case with @ 60 cases.  Which is better, or are there better ways?
PS: Since the switch only has 4 outputs, I have a version that has only 4 cases. 

Mike King

  • Diamond Member
  • *****
  • Posts: 3810
  • Mike King
    • View Profile
    • BBSysco Consulting
Re: Read Data to a memory File or just use a switch?
« Reply #1 on: January 20, 2022, 03:12:31 PM »
One thing I have done in the past when I needed a quick lookup table that was easy to edit/maintain was simply create a text file with the values and use the POS to do a quick scan.

For example you could have a file like:

# These are the XYC codes and values
ABC=First three letters
LMN=Not the first three
MFK=My initials
007=James Bond (deceased)

Put these in a text file the used the *tools/readfile utility to do a binary read of the whole file into memory.
Scan for $0a$+code$+"=" where code$ is the code you are looking for.
Once found pull out the value starting after the "=" up to a $0d$ or $0a$

Something like:

Code: [Select]
ofs = POS( $0A$+code$+"=" = filecontents$ )
if ofs = 0 then ...not found...
ofs += 1 + len(code$) + 1
val$ = filecontents$(ofs, POS($0d0a$:filecontents$(ofs) )

Make sure last line in file is properly terminated with LF -- most editors will do this but for safety add $0A$ after reading the file contents.
Also make sure to leave a comment line at the start as it will provide the initial line feed
Mike King
President - BBSysco Consulting
eMail: mike.king@bbsysco.com

Peter.Higgins

  • Diamond Member
  • *****
  • Posts: 124
    • View Profile
Re: Read Data to a memory File or just use a switch?
« Reply #2 on: January 20, 2022, 03:46:23 PM »
Thanks Mike, great answer as always.
That brought back memories of working on the Music title multi-word match lookup back in the day where I used POS to keep the key chain and iterative reads small.
Also made me realize JSON string to an associative array is yet another way a text file could work.

Mike King

  • Diamond Member
  • *****
  • Posts: 3810
  • Mike King
    • View Profile
    • BBSysco Consulting
Re: Read Data to a memory File or just use a switch?
« Reply #3 on: January 20, 2022, 04:22:27 PM »
Yes JSON could also be used, but I like keeping things as simple as possible. 
If you go the JSON route you have to train whoever will maintain/edit this file on the layout, formatting and how to handle escaping things like < and >. 

Simple text files are really hard to mess up.
Mike King
President - BBSysco Consulting
eMail: mike.king@bbsysco.com

Peter.Higgins

  • Diamond Member
  • *****
  • Posts: 124
    • View Profile
Re: Read Data to a memory File or just use a switch?
« Reply #4 on: January 21, 2022, 09:21:58 AM »
True, simple is invariably the best choice.