Thanks all for your helpful replies. In order to test the performance, I
wrote the following 3 codes to parse:

Input string is Inp_Fld. Input does not start with any control character.

1.
// Using %scan and %subst
Pos = 0;
Start = 1;
V_Fld = %trimr(%xlate(x'1d1e' : ' ' : Inp_Fld)); //these are always at
the end of string and just before 1c, so made them blank
DoU Pos = 0;
Pos = %scan(x'1c' : V_Fld : Start);
If Pos > 0;
Field = %subst(V_Fld : Start : Pos - Start);
Start = Pos + 1;
// will insert logic to use Field
EndIf;
EndDo;
Field = %subst(V_Fld : Start);

2.
// Using %split
For-Each Field in %split(Inp_Fld : x'1c1d1e');
// will insert logic to use Field
EndFor;

3.
// Using Pointers and scanning the full string
p1 = %addr(Inp_Fld);
Len = %len(%trimr(Inp_Fld));
p3 = p1;
For Ind = 1 To Len;
p2 = p1 + Ind;
If %str(p2:1) = x'1c' Or %str(p2:1) = x'1d' Or %str(p2:1) = x'1e' Or
Ind = Len;
If (p2-p3) > 1;
Field = %str(p3 : p2 - p3);
// will insert logic to use Field
EndIf;
p3 = p2 + 1;
EndIf;
EndFor;

For a given string (about 300 bytes with 34 fields, which is expected to be
the average), I iterated each of them 10000 times and captured the time
taken by each:
#1 - 70 milli-seconds
#2 - 86 milli-seconds
#3 - 712 milli-seconds

Not sure if my code for #3 is not efficient, but did not expect it to be
that bad. Even though #2 has the simplest code, I would probably go with #1.
Thanks again.


On Fri, Jun 3, 2022 at 6:53 PM Barbara Morris <bmorris@xxxxxxxxxx> wrote:

On 2022-06-03 12:04 p.m., Vinay Gavankar wrote:
...
Would it be more efficient to use %scan to find the start and end
positions
and then %subst to extract the value, or use some sort of pointer code to
go thru the string character by character to do my own search? Or use a
combination of both?

My first scan to find the start of the field is simple. But to get the
end
of field, I will have to do multiple %scans for each of the 3 possible
characters (unless I am missing something).
...

I would use %XLATE to get a temporary with all the 3 possible end
characters translated to the one of the end characters.

temp = %xlate(x'1d1e' : x'1c1c' : string);

And then use %SCAN on the temp string just looking for x'1c'. If you
need to know what actual end character it was, you still have the
original string.

Jon's idea to use %SPLIT sounds good too.

--
Barbara

--
This is the RPG programming on IBM i (RPG400-L) mailing list
To post a message email: RPG400-L@xxxxxxxxxxxxxxxxxx
To subscribe, unsubscribe, or change list options,
visit: https://lists.midrange.com/mailman/listinfo/rpg400-l
or email: RPG400-L-request@xxxxxxxxxxxxxxxxxx
Before posting, please take a moment to review the archives
at https://archive.midrange.com/rpg400-l.

Please contact support@xxxxxxxxxxxxxxxxxxxx for any subscription related
questions.

Help support midrange.com by shopping at amazon.com with our affiliate
link: https://amazon.midrange.com


As an Amazon Associate we earn from qualifying purchases.

This thread ...

Follow-Ups:
Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.