PxPlus User Forum

Twitter Twitter Twitter

Author Topic: Easy way to split a numeric fixed string.  (Read 1997 times)

Peter.Higgins

  • Diamond Member
  • *****
  • Posts: 124
    • View Profile
Easy way to split a numeric fixed string.
« on: December 05, 2019, 12:58:19 PM »
I was looking at some complicated date logic and thought of contributing this simple efficient technique example.   This uses the BBX string structures in PVX. 
The numeric and formatting validation is omitted.

1 DIM Dt$:"Y:N(4),M:N(2),D:N(2)" ! Structure is static and can be reused after ="" or read data from $$
2 Dt$="20191105"
3 ? jul(Dt.Y,Dt.M,Dt.D)
4 ? xfa(Dt$,"") ! Roughly equivalent to LST(IOL())
5 ?Dt.Y
6 ?Dt.M
7 ?Dt.D




Mike King

  • Diamond Member
  • *****
  • Posts: 3810
  • Mike King
    • View Profile
    • BBSysco Consulting
Re: Easy way to split a numeric fixed string.
« Reply #1 on: December 05, 2019, 01:18:12 PM »
Why not just:

READ DATA FROM DTE(dt:"%Y-%M-%D"),SEP="-" TO Yr,Mon,Dy

Technically you could add time of day as in:

READ DATA FROM DTE(dt:"%Y-%M-%D-%H-%m-%s"),SEP="-" TO Yr,Mon,Dy,Hr,Min,Sec

Mike King
President - BBSysco Consulting
eMail: mike.king@bbsysco.com

Peter.Higgins

  • Diamond Member
  • *****
  • Posts: 124
    • View Profile
Re: Easy way to split a numeric fixed string.
« Reply #2 on: December 05, 2019, 05:32:38 PM »
Mike,

Read data works well for Julian (your example) or without DTE on a formatted date. 
I am guessing but I suspect for String to Number, my original technique and the two PVX equivalents at the bottom of this reply will be very hard to beat on efficiency. 

Good reasons to use a structured (or composite) string technique are:
 1: because the original data is an un-formatted string and both original and parsed data are needed.
 2: A Variable fits better than a formula into a IOLIST and accepts assignment which a formula can not.
 3: in a tight loop of thousands/millions of records, READ DATA will run noticeably slower than either
     multiple num(substring) or the structured string approach. (2002 list date functions thread)
 4: Read/Assignment results can be fed directly into functions with implicit conversion.
     Dt$=DTM06$,DayOfWeek=NUM(DTE(JUL(Dt.Y,Dt.M,Dt.D):"%W") )
 5: Only one Dim of a string structure is necessary.  CLEAR or DIM must be used to remove\change structure.

Re # 3:  Your comments on the relative efficiency of these approaches will definitely be appreciated.

While not quite as simple and easy to remember as :N(len), strings to numbers can also be achieved with PVX composite string structures as in:

1 DIM Dt$:CPL("IOLIST Y:[NUM(4)],M:[NUM(2)],D:[NUM(2)]")
2 !
3 Dt$="20191105"
4 ?Dt.Y
5 ?Dt.M
6 ?Dt.D

1 IOL.dt: IOLIST Y:[NUM(4)],M:[NUM(2)],D:[NUM(2)]
2 DIM Dt$:iol=iol.dt
3 Dt$="20191105"
4 ?Dt.Y
5 ?Dt.M
6 ?Dt.D