![]() | |
#1
| |||
| |||
|
|
RUN BP DT.TEST |
#2
| |||
| |||
|
|
Hi, I want to assess the data type of a string into 3 basic categories: numeric, date, and text. I wrote a user defined function to do this (test program shown below) which works OK in most circumstances. However, when it is passed the string "1/2 SCOTTS" it assesses the string to be of type DATE - ie 1/2/05. Does anyone have any ideas for a test for a date string that doesn't fall into this trap? Platform = mvBASE. Thanks, Brian Speirs. Program DT.TEST FUNCTION DATA.TYPE(DATA.STRING) DT = "T"; * Text (default) IF DATA.STRING NE "" THEN IF NUM(DATA.STRING) THEN DT = "N"; * Numeric IF DT = "T" THEN IF ICONV(DATA.STRING, 'D') NE "" THEN DT = "D" END END RETURNING DT LOOP CRT "Enter data: ": INPUT TESTDATA: UNTIL TESTDATA = "X" OR TESTDATA = "x" DO DATATYPE = @DATA.TYPE(TESTDATA) CRT " Data type = " ATATYPEREPEAT STOP END RUN BP DT.TEST Enter data: ?HELLO Data type = T Enter data: ?125 Data type = N Enter data: ?125.123 Data type = N Enter data: ?31/5/05 Data type = D Enter data: ?1/2 Data type = D Enter data: ?1/2 DOZ Data type = D Enter data: ?X -- ************************************************** * Brian Speirs h: (04) 479 9032 c: (021) 265 5906 e: bss59REMOVETHIS (AT) paradise (DOT) net.nz |
#3
| |||
| |||
|
|
Brian, I don't have mvBASE up and running so I needed to do this with a subroutine instead of a function. The following code may help solve your problem. Note the use of the OCONV for MCA which has to be Text. *Program DATATYPE * 06/17/05 Dave Weaver, Weaver Consulting *============================================== PROMPT '' LOOP CRT "Enter data: ": INPUT TESTDATA: UNTIL TESTDATA = "X" OR TESTDATA = "x" DO GOSUB 100 CRT " Data type = " ATATYPEREPEAT STOP * 100 * Develop the DATATYPE BEGIN CASE CASE TESTDATA = '' DATATYPE = 'T' CASE INDEX(TESTDATA,' ',1) DATATYPE = 'T' CASE OCONV(TESTDATA,'MCA') # '' DATATYPE = 'T' CASE NUM(TESTDATA) DATATYPE = 'N' CASE ICONV(TESTDATA,'D') # '' DATATYPE = 'D' CASE 1 DATATYPE = 'ERR' END CASE RETURN * END Brian Speirs wrote: Hi, I want to assess the data type of a string into 3 basic categories: numeric, date, and text. I wrote a user defined function to do this (test program shown below) which works OK in most circumstances. However, when it is passed the string "1/2 SCOTTS" it assesses the string to be of type DATE - ie 1/2/05. Does anyone have any ideas for a test for a date string that doesn't fall into this trap? Platform = mvBASE. Thanks, Brian Speirs. Program DT.TEST FUNCTION DATA.TYPE(DATA.STRING) DT = "T"; * Text (default) IF DATA.STRING NE "" THEN IF NUM(DATA.STRING) THEN DT = "N"; * Numeric IF DT = "T" THEN IF ICONV(DATA.STRING, 'D') NE "" THEN DT = "D" END END RETURNING DT LOOP CRT "Enter data: ": INPUT TESTDATA: UNTIL TESTDATA = "X" OR TESTDATA = "x" DO DATATYPE = @DATA.TYPE(TESTDATA) CRT " Data type = " ATATYPEREPEAT STOP END RUN BP DT.TEST Enter data: ?HELLO Data type = T Enter data: ?125 Data type = N Enter data: ?125.123 Data type = N Enter data: ?31/5/05 Data type = D Enter data: ?1/2 Data type = D Enter data: ?1/2 DOZ Data type = D Enter data: ?X -- ************************************************** * Brian Speirs h: (04) 479 9032 c: (021) 265 5906 e: bss59REMOVETHIS (AT) paradise (DOT) net.nz |
#4
| |||
| |||
|
|
I see some problems here. Take the following sample run... Enter data: 1/1*!$# Data type = D Enter data: Data type = T Enter data: *!@ Data type = ERR Hmmm. <g Luke Dave Weaver wrote: Brian, I don't have mvBASE up and running so I needed to do this with a subroutine instead of a function. The following code may help solve your problem. Note the use of the OCONV for MCA which has to be Text. *Program DATATYPE * 06/17/05 Dave Weaver, Weaver Consulting *============================================== PROMPT '' LOOP CRT "Enter data: ": INPUT TESTDATA: UNTIL TESTDATA = "X" OR TESTDATA = "x" DO GOSUB 100 CRT " Data type = " ATATYPEREPEAT STOP * 100 * Develop the DATATYPE BEGIN CASE CASE TESTDATA = '' DATATYPE = 'T' CASE INDEX(TESTDATA,' ',1) DATATYPE = 'T' CASE OCONV(TESTDATA,'MCA') # '' DATATYPE = 'T' CASE NUM(TESTDATA) DATATYPE = 'N' CASE ICONV(TESTDATA,'D') # '' DATATYPE = 'D' CASE 1 DATATYPE = 'ERR' END CASE RETURN * END Brian Speirs wrote: Hi, I want to assess the data type of a string into 3 basic categories: numeric, date, and text. I wrote a user defined function to do this (test program shown below) which works OK in most circumstances. However, when it is passed the string "1/2 SCOTTS" it assesses the string to be of type DATE - ie 1/2/05. Does anyone have any ideas for a test for a date string that doesn't fall into this trap? Platform = mvBASE. Thanks, Brian Speirs. Program DT.TEST FUNCTION DATA.TYPE(DATA.STRING) DT = "T"; * Text (default) IF DATA.STRING NE "" THEN IF NUM(DATA.STRING) THEN DT = "N"; * Numeric IF DT = "T" THEN IF ICONV(DATA.STRING, 'D') NE "" THEN DT = "D" END END RETURNING DT LOOP CRT "Enter data: ": INPUT TESTDATA: UNTIL TESTDATA = "X" OR TESTDATA = "x" DO DATATYPE = @DATA.TYPE(TESTDATA) CRT " Data type = " ATATYPEREPEAT STOP END RUN BP DT.TEST Enter data: ?HELLO Data type = T Enter data: ?125 Data type = N Enter data: ?125.123 Data type = N Enter data: ?31/5/05 Data type = D Enter data: ?1/2 Data type = D Enter data: ?1/2 DOZ Data type = D Enter data: ?X -- ************************************************** * Brian Speirs h: (04) 479 9032 c: (021) 265 5906 e: bss59REMOVETHIS (AT) paradise (DOT) net.nz |
#5
| |||
| |||
|
|
Good eye, Luke! I also found some problems with my original code. THE REVISED VERSION FOLLOWS. Note that ICONV of something not null is NOT a valid date test! Once ICONVed, the ICONVed value must be OCONVed to NOT NULL to be a valid date! Note this code: *Program DATATYPE * 06/17/05 Dave Weaver, Weaver Consulting *============================================== PROMPT '' LOOP CRT "Enter data: ": INPUT TESTDATA: UNTIL TESTDATA = "X" OR TESTDATA = "x" DO GOSUB 100 CRT " Data type = " ATATYPEREPEAT STOP * 100 * Develop the DATATYPE DATETEST = ICONV(TESTDATA,'D') ; DATETEST = OCONV(DATETEST,"D2/") BEGIN CASE CASE TESTDATA = '' ;* NULL is Text DATATYPE = 'T' CASE INDEX(TESTDATA,' ',1) ;* At least one space DATATYPE = 'T' CASE OCONV(TESTDATA,'MCA') # '' ;* Alpha is not null DATATYPE = 'T' CASE NUM(TESTDATA) ;* Is numeric DATATYPE = 'N' CASE DATETEST # '' ;* OCONV DATE is not null DATATYPE = 'D' CASE 1 DATATYPE = 'ERR' END CASE RETURN * END Luke Webber wrote: I see some problems here. Take the following sample run... Enter data: 1/1*!$# Data type = D Enter data: Data type = T Enter data: *!@ Data type = ERR Hmmm. <g Luke Dave Weaver wrote: Brian, I don't have mvBASE up and running so I needed to do this with a subroutine instead of a function. The following code may help solve your problem. Note the use of the OCONV for MCA which has to be Text. *Program DATATYPE * 06/17/05 Dave Weaver, Weaver Consulting *============================================== PROMPT '' LOOP CRT "Enter data: ": INPUT TESTDATA: UNTIL TESTDATA = "X" OR TESTDATA = "x" DO GOSUB 100 CRT " Data type = " ATATYPEREPEAT STOP * 100 * Develop the DATATYPE BEGIN CASE CASE TESTDATA = '' DATATYPE = 'T' CASE INDEX(TESTDATA,' ',1) DATATYPE = 'T' CASE OCONV(TESTDATA,'MCA') # '' DATATYPE = 'T' CASE NUM(TESTDATA) DATATYPE = 'N' CASE ICONV(TESTDATA,'D') # '' DATATYPE = 'D' CASE 1 DATATYPE = 'ERR' END CASE RETURN * END Brian Speirs wrote: Hi, I want to assess the data type of a string into 3 basic categories: numeric, date, and text. I wrote a user defined function to do this (test program shown below) which works OK in most circumstances. However, when it is passed the string "1/2 SCOTTS" it assesses the string to be of type DATE - ie 1/2/05. Does anyone have any ideas for a test for a date string that doesn't fall into this trap? Platform = mvBASE. Thanks, Brian Speirs. Program DT.TEST FUNCTION DATA.TYPE(DATA.STRING) DT = "T"; * Text (default) IF DATA.STRING NE "" THEN IF NUM(DATA.STRING) THEN DT = "N"; * Numeric IF DT = "T" THEN IF ICONV(DATA.STRING, 'D') NE "" THEN DT = "D" END END RETURNING DT LOOP CRT "Enter data: ": INPUT TESTDATA: UNTIL TESTDATA = "X" OR TESTDATA = "x" DO DATATYPE = @DATA.TYPE(TESTDATA) CRT " Data type = " ATATYPEREPEAT STOP END RUN BP DT.TEST Enter data: ?HELLO Data type = T Enter data: ?125 Data type = N Enter data: ?125.123 Data type = N Enter data: ?31/5/05 Data type = D Enter data: ?1/2 Data type = D Enter data: ?1/2 DOZ Data type = D Enter data: ?X -- ************************************************** * Brian Speirs h: (04) 479 9032 c: (021) 265 5906 e: bss59REMOVETHIS (AT) paradise (DOT) net.nz |
#6
| |||
| |||
|
|
One more change and I think we have it! In the CASE 1 section, change 'ERR' to 'T' for text. Now, I can't make it fail! Dave Weaver Dave Weaver wrote: Good eye, Luke! I also found some problems with my original code. THE REVISED VERSION FOLLOWS. Note that ICONV of something not null is NOT a valid date test! Once ICONVed, the ICONVed value must be OCONVed to NOT NULL to be a valid date! Note this code: *Program DATATYPE * 06/17/05 Dave Weaver, Weaver Consulting *============================================== PROMPT '' LOOP CRT "Enter data: ": INPUT TESTDATA: UNTIL TESTDATA = "X" OR TESTDATA = "x" DO GOSUB 100 CRT " Data type = " ATATYPEREPEAT STOP * 100 * Develop the DATATYPE DATETEST = ICONV(TESTDATA,'D') ; DATETEST = OCONV(DATETEST,"D2/") BEGIN CASE CASE TESTDATA = '' ;* NULL is Text DATATYPE = 'T' CASE INDEX(TESTDATA,' ',1) ;* At least one space DATATYPE = 'T' CASE OCONV(TESTDATA,'MCA') # '' ;* Alpha is not null DATATYPE = 'T' CASE NUM(TESTDATA) ;* Is numeric DATATYPE = 'N' CASE DATETEST # '' ;* OCONV DATE is not null DATATYPE = 'D' CASE 1 DATATYPE = 'ERR' END CASE RETURN * END Luke Webber wrote: I see some problems here. Take the following sample run... Enter data: 1/1*!$# Data type = D Enter data: Data type = T Enter data: *!@ Data type = ERR Hmmm. <g Luke Dave Weaver wrote: Brian, I don't have mvBASE up and running so I needed to do this with a subroutine instead of a function. The following code may help solve your problem. Note the use of the OCONV for MCA which has to be Text. *Program DATATYPE * 06/17/05 Dave Weaver, Weaver Consulting *============================================== PROMPT '' LOOP CRT "Enter data: ": INPUT TESTDATA: UNTIL TESTDATA = "X" OR TESTDATA = "x" DO GOSUB 100 CRT " Data type = " ATATYPEREPEAT STOP * 100 * Develop the DATATYPE BEGIN CASE CASE TESTDATA = '' DATATYPE = 'T' CASE INDEX(TESTDATA,' ',1) DATATYPE = 'T' CASE OCONV(TESTDATA,'MCA') # '' DATATYPE = 'T' CASE NUM(TESTDATA) DATATYPE = 'N' CASE ICONV(TESTDATA,'D') # '' DATATYPE = 'D' CASE 1 DATATYPE = 'ERR' END CASE RETURN * END Brian Speirs wrote: Hi, I want to assess the data type of a string into 3 basic categories: numeric, date, and text. I wrote a user defined function to do this (test program shown below) which works OK in most circumstances. However, when it is passed the string "1/2 SCOTTS" it assesses the string to be of type DATE - ie 1/2/05. Does anyone have any ideas for a test for a date string that doesn't fall into this trap? Platform = mvBASE. Thanks, Brian Speirs. Program DT.TEST FUNCTION DATA.TYPE(DATA.STRING) DT = "T"; * Text (default) IF DATA.STRING NE "" THEN IF NUM(DATA.STRING) THEN DT = "N"; * Numeric IF DT = "T" THEN IF ICONV(DATA.STRING, 'D') NE "" THEN DT = "D" END END RETURNING DT LOOP CRT "Enter data: ": INPUT TESTDATA: UNTIL TESTDATA = "X" OR TESTDATA = "x" DO DATATYPE = @DATA.TYPE(TESTDATA) CRT " Data type = " ATATYPEREPEAT STOP END RUN BP DT.TEST Enter data: ?HELLO Data type = T Enter data: ?125 Data type = N Enter data: ?125.123 Data type = N Enter data: ?31/5/05 Data type = D Enter data: ?1/2 Data type = D Enter data: ?1/2 DOZ Data type = D Enter data: ?X -- ************************************************** * Brian Speirs h: (04) 479 9032 c: (021) 265 5906 e: bss59REMOVETHIS (AT) paradise (DOT) net.nz |
#7
| |||
| |||
|
|
Hi, I want to assess the data type of a string into 3 basic categories: numeric, date, and text. I wrote a user defined function to do this (test program shown below) which works OK in most circumstances. However, when it is passed the string "1/2 SCOTTS" it assesses the string to be of type DATE - ie 1/2/05. Does anyone have any ideas for a test for a date string that doesn't fall into this trap? Platform = mvBASE. Thanks, Brian Speirs. Program DT.TEST FUNCTION DATA.TYPE(DATA.STRING) DT = "T"; * Text (default) IF DATA.STRING NE "" THEN IF NUM(DATA.STRING) THEN DT = "N"; * Numeric IF DT = "T" THEN IF ICONV(DATA.STRING, 'D') NE "" THEN DT = "D" END END RETURNING DT LOOP CRT "Enter data: ": INPUT TESTDATA: UNTIL TESTDATA = "X" OR TESTDATA = "x" DO DATATYPE = @DATA.TYPE(TESTDATA) CRT " Data type = " ATATYPEREPEAT STOP END RUN BP DT.TEST Enter data: ?HELLO Data type = T Enter data: ?125 Data type = N Enter data: ?125.123 Data type = N Enter data: ?31/5/05 Data type = D Enter data: ?1/2 Data type = D Enter data: ?1/2 DOZ Data type = D Enter data: ?X |
#8
| |||
| |||
|
|
Goo'day, Briian, I've got no way of testing this under your rules, but I'd be very tempted to change your"ICONV" line to IF OCONV(ICONV(DATA,'D'),'D') = DATA THEN DT = 'D' in other words convert in and then convert that out and if that's the same as the original you're laughing..... On Wed, 15 Jun 2005 11:53:56 +1200, Brian Speirs bss59REMOVE_THIS (AT) paradise (DOT) net.nz> wrote: Hi, I want to assess the data type of a string into 3 basic categories: numeric, date, and text. I wrote a user defined function to do this (test program shown below) which works OK in most circumstances. However, when it is passed the string "1/2 SCOTTS" it assesses the string to be of type DATE - ie 1/2/05. Does anyone have any ideas for a test for a date string that doesn't fall into this trap? Platform = mvBASE. Thanks, Brian Speirs. Program DT.TEST FUNCTION DATA.TYPE(DATA.STRING) DT = "T"; * Text (default) IF DATA.STRING NE "" THEN IF NUM(DATA.STRING) THEN DT = "N"; * Numeric IF DT = "T" THEN IF ICONV(DATA.STRING, 'D') NE "" THEN DT = "D" END END RETURNING DT LOOP CRT "Enter data: ": INPUT TESTDATA: UNTIL TESTDATA = "X" OR TESTDATA = "x" DO DATATYPE = @DATA.TYPE(TESTDATA) CRT " Data type = " ATATYPEREPEAT STOP END RUN BP DT.TEST Enter data: ?HELLO Data type = T Enter data: ?125 Data type = N Enter data: ?125.123 Data type = N Enter data: ?31/5/05 Data type = D Enter data: ?1/2 Data type = D Enter data: ?1/2 DOZ Data type = D Enter data: ?X Regards, Bruce Nichol Talon Computer Services ALBURY NSW Australia http://www.taloncs.com.au If it ain't broke, fix it until it is.... |
#9
| |||
| |||
|
|
I'm sure that whatever rules we come up with, there will be cases when dates and strings will be confused! no: |
|
Thanks for this. This solves my immediate problem of making sure that text is correctly displayed in an html report. But I did note that strings such as 1!2 or 1!2!05 are counted as dates. And that 01 JUN 2005 is counted as a string! Cheers, Brian Dave Weaver wrote: One more change and I think we have it! In the CASE 1 section, change 'ERR' to 'T' for text. Now, I can't make it fail! Dave Weaver Dave Weaver wrote: Good eye, Luke! I also found some problems with my original code. THE REVISED VERSION FOLLOWS. Note that ICONV of something not null is NOT a valid date test! Once ICONVed, the ICONVed value must be OCONVed to NOT NULL to be a valid date! Note this code: *Program DATATYPE * 06/17/05 Dave Weaver, Weaver Consulting *============================================== PROMPT '' LOOP CRT "Enter data: ": INPUT TESTDATA: UNTIL TESTDATA = "X" OR TESTDATA = "x" DO GOSUB 100 CRT " Data type = " ATATYPEREPEAT STOP * 100 * Develop the DATATYPE DATETEST = ICONV(TESTDATA,'D') ; DATETEST = OCONV(DATETEST,"D2/") BEGIN CASE CASE TESTDATA = '' ;* NULL is Text DATATYPE = 'T' CASE INDEX(TESTDATA,' ',1) ;* At least one space DATATYPE = 'T' CASE OCONV(TESTDATA,'MCA') # '' ;* Alpha is not null DATATYPE = 'T' CASE NUM(TESTDATA) ;* Is numeric DATATYPE = 'N' CASE DATETEST # '' ;* OCONV DATE is not null DATATYPE = 'D' CASE 1 DATATYPE = 'ERR' END CASE RETURN * END Luke Webber wrote: I see some problems here. Take the following sample run... Enter data: 1/1*!$# Data type = D Enter data: Data type = T Enter data: *!@ Data type = ERR Hmmm. <g Luke Dave Weaver wrote: Brian, I don't have mvBASE up and running so I needed to do this with a subroutine instead of a function. The following code may help solve your problem. Note the use of the OCONV for MCA which has to be Text. *Program DATATYPE * 06/17/05 Dave Weaver, Weaver Consulting *============================================== PROMPT '' LOOP CRT "Enter data: ": INPUT TESTDATA: UNTIL TESTDATA = "X" OR TESTDATA = "x" DO GOSUB 100 CRT " Data type = " ATATYPEREPEAT STOP * 100 * Develop the DATATYPE BEGIN CASE CASE TESTDATA = '' DATATYPE = 'T' CASE INDEX(TESTDATA,' ',1) DATATYPE = 'T' CASE OCONV(TESTDATA,'MCA') # '' DATATYPE = 'T' CASE NUM(TESTDATA) DATATYPE = 'N' CASE ICONV(TESTDATA,'D') # '' DATATYPE = 'D' CASE 1 DATATYPE = 'ERR' END CASE RETURN * END Brian Speirs wrote: Hi, I want to assess the data type of a string into 3 basic categories: numeric, date, and text. I wrote a user defined function to do this (test program shown below) which works OK in most circumstances. However, when it is passed the string "1/2 SCOTTS" it assesses the string to be of type DATE - ie 1/2/05. Does anyone have any ideas for a test for a date string that doesn't fall into this trap? Platform = mvBASE. Thanks, Brian Speirs. Program DT.TEST FUNCTION DATA.TYPE(DATA.STRING) DT = "T"; * Text (default) IF DATA.STRING NE "" THEN IF NUM(DATA.STRING) THEN DT = "N"; * Numeric IF DT = "T" THEN IF ICONV(DATA.STRING, 'D') NE "" THEN DT = "D" END END RETURNING DT LOOP CRT "Enter data: ": INPUT TESTDATA: UNTIL TESTDATA = "X" OR TESTDATA = "x" DO DATATYPE = @DATA.TYPE(TESTDATA) CRT " Data type = " ATATYPEREPEAT STOP END RUN BP DT.TEST Enter data: ?HELLO Data type = T Enter data: ?125 Data type = N Enter data: ?125.123 Data type = N Enter data: ?31/5/05 Data type = D Enter data: ?1/2 Data type = D Enter data: ?1/2 DOZ Data type = D Enter data: ?X -- ************************************************** * Brian Speirs h: (04) 479 9032 c: (021) 265 5906 e: bss59REMOVETHIS (AT) paradise (DOT) net.nz -- ************************************************** * Brian Speirs h: (04) 479 9032 c: (021) 265 5906 e: bss59REMOVETHIS (AT) paradise (DOT) net.nz |
#10
| |||
| |||
|
|
What I would do... TESTDATA = OCONV(OCONV(INDATA,'MC/A'),'MC/N') In this way you will end up with only the delimiters, invalid chars etc. If you check the length of what's in TESTDATA then and its other than 1 character long, it will mean that its a string, not a date... try it, MC/ got me out of trouble often jak Bruce Nichol wrote: Goo'day, Briian, I've got no way of testing this under your rules, but I'd be very tempted to change your"ICONV" line to IF OCONV(ICONV(DATA,'D'),'D') = DATA THEN DT = 'D' in other words convert in and then convert that out and if that's the same as the original you're laughing..... On Wed, 15 Jun 2005 11:53:56 +1200, Brian Speirs bss59REMOVE_THIS (AT) paradise (DOT) net.nz> wrote: Hi, I want to assess the data type of a string into 3 basic categories: numeric, date, and text. I wrote a user defined function to do this (test program shown below) which works OK in most circumstances. However, when it is passed the string "1/2 SCOTTS" it assesses the string to be of type DATE - ie 1/2/05. Does anyone have any ideas for a test for a date string that doesn't fall into this trap? Platform = mvBASE. Thanks, Brian Speirs. Program DT.TEST FUNCTION DATA.TYPE(DATA.STRING) DT = "T"; * Text (default) IF DATA.STRING NE "" THEN IF NUM(DATA.STRING) THEN DT = "N"; * Numeric IF DT = "T" THEN IF ICONV(DATA.STRING, 'D') NE "" THEN DT = "D" END END RETURNING DT LOOP CRT "Enter data: ": INPUT TESTDATA: UNTIL TESTDATA = "X" OR TESTDATA = "x" DO DATATYPE = @DATA.TYPE(TESTDATA) CRT " Data type = " ATATYPEREPEAT STOP END RUN BP DT.TEST Enter data: ?HELLO Data type = T Enter data: ?125 Data type = N Enter data: ?125.123 Data type = N Enter data: ?31/5/05 Data type = D Enter data: ?1/2 Data type = D Enter data: ?1/2 DOZ Data type = D Enter data: ?X Regards, Bruce Nichol Talon Computer Services ALBURY NSW Australia http://www.taloncs.com.au If it ain't broke, fix it until it is.... |
![]() |
| Thread Tools | |
| Display Modes | |
| |