Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Speed this Code Up?
Message
From
05/07/2006 08:32:07
 
 
To
All
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Title:
Speed this Code Up?
Environment versions
Visual FoxPro:
VFP 9
OS:
Windows XP SP2
Network:
Windows 2003 Server
Database:
MS SQL Server
Miscellaneous
Thread ID:
01133775
Message ID:
01133775
Views:
62
The below code works and is located in the form init, however, I would like to speed it up if possible. Basically, a person can type on a command line something similar to:

B10 TS 2004, TX, ABCD123, 109 N MAIN ST, 62" 132LBS MORE COMMENTS

From left to right, the values signify, an id, the command, vehicle license tag, location, height,
weight, and additional comments. Actually the height and weight are part of the comments.

Ideally the user would always enter information in this order:

id
vehicle license tag
vehicle license tag state
vehicle license tag year
location
comments

that is the order of the fields on the form itself which are populated with the values the user entered. Every separate field must be separated by a comma (to parse). Later it was decided that the user should be able to enter the values to populate the fields in any order after the TS is typed. So, anything after "TS" denotes data to populate the fields on the form. The "TS" tells the system which form to pass the values to. It is working, but this change means that I had to change the logic - for instance the user must be allowed to type a 2-digit or 4-digit year. Some vehicle tags have information which can be similar to a year (e.g. 4004 for a motorcycle tag). If the value entered equals a year less than three years old and no further into the future than 1 year, I can assume it is the vehicle year and not the vehicle tag (for our purposes). However, to determine that, I have to check for the year before anything else is done so that I can process it as a vehicle tag if it doesn't match the > = year(date())-3 and < = year(date())+1 check. A vehicle tag cannot contain spaces (to differentiate it from the comments field). A location must always start with a number and contain at least one space (e.g. 100 N Main) or have no number but include a forward or backward slash (e.g. Main/Green). Although there is no specific field for height, weight, race, gender, etc, the user may type that information and it should go into the comments field. Anything not valid should go into the comments field.

Ok, given the above, the code below works. However I would like to speed it up if possible. When the form loads, an image of the form displays before it is completely solid and populated with the values. The code has evolved over the changing of the requirements and I have probably looked at it too much so I am putting it out there for any takers to have fun with and see if the speed can be improved.
        *--Examples of tcUserTyped:
        * B10 TS 4004, 100 N Main St, TX, 05, WM, 132lbs, 62", More Comments
        * C32 TS ABC123, TX, 2005, 100 N MAIN ST, 2 CHILDREN IN VEH
        * A10 TS 100 N MAIN ST, ABC123, SC, 06, WM 150LBS 60"
        * C10 TS SC, 4003, 04, 100 N MAIN ST
        * In the last example, 4003 is the vehicle tag and 2004 the year
	vstoploc = .T. 	                     && check for location
	vstoplnpos = AT('TS',tcUserTyped)    && determine where to start parsing
	vstopik = 0                          && used for counting parsed values items
	vstoplcstring = ""                   && temporary string to store the parsed results to
	IF vstoplnpos > 0                    && IF "TS" is in the user typed value, continue
		FOR istep = vstoplnpos+2 TO LEN(RTRIM(tcUserTyped))
			IF SUBSTR(tcUserTyped,istep,1) = ","
				vstopik = vstopik + 1
				DIMENSION trafficinfo(vstopik,2)	&& store the parsed value into an array element
				trafficinfo(vstopik,1) = vstoplcstring
				trafficinfo(vstopik,2) = ""
				vstoplcstring = ""
			ELSE
				vstoplcstring = vstoplcstring + SUBSTR(tcUserTyped,istep,1)
			ENDIF
		ENDFOR
		*--Last item in the string get it as well
		IF !EMPTY(ALLTRIM(vstoplcstring))
			vstopik = vstopik + 1
			DIMENSION trafficinfo(vstopik,2)
			trafficinfo(vstopik,1) = vstoplcstring
			trafficinfo(vstopik,2) = ""
			vstoplcstring = ""
		ENDIF
	ENDIF
	IF TYPE('alen(trafficinfo,1)')="N" .AND. ALEN(trafficinfo,1) > 0	&& valid traffic stop info entered
		vstoploc = .T. 			&& check for location
		vstoplicensetag = .T. 	&& check for license
		vstoplicenseyr = .T.	&& check for the license year
		vstoplicensest = .T.	&& check for the license state
		vstopvalue = ""			&& temporary var used to store the array element for checking
		vstopnlicvalue = 0          && temporary var used to verify if 2 digit year or license passed
		vstopclicvalue = ''         && temporary var used to verify if 2 digit year or license passed

		FOR istep2 = 1 TO ALEN(trafficinfo,1)
		
			IF TYPE('trafficinfo(istep2,1)') = "C" .AND. !EMPTY(trafficinfo(istep2,1))
				
				vstopvalue = ALLTRIM(trafficinfo(istep2,1))
				
				*--2 digit - if the license year has not been processed yet, is it a year or a license tag number
				*--Any two digit value  > 0 gets processed as license number or a year in the case below
				IF vstoplicenseyr .and. LEN(vstopvalue) = 2 .and. VAL(vstopvalue) > 0 && 01-99
				   vstopnlicvalue=VAL(vstopvalue)
				   IF vstopnlicvalue<100 AND vstopnlicvalue>0
					  IF vstopnlicvalue<gnCentRollover
						 vstopclicvalue=LTRIM(STR(2000+vstopnlicvalue))
					  ELSE
						 vstopclicvalue=LTRIM(STR(1900+vstopnlicvalue))
					  ENDIF
	  				  IF (LEN(vstopclicvalue) = 4 ;
							.AND. INT(VAL(vstopclicvalue)) >= YEAR(DATE())-3 ;
							.AND. INT(VAL(vstopclicvalue)) <= YEAR(DATE())+1) && three years ago to 1 year in the future
						  vstopvalue = vstopclicvalue
					  ENDIF
				   ENDIF
				ENDIF	

				DO CASE

					*--Verify speed after with check for ' " may be slow drawing screen when doing string comparison
					CASE CHR(34) $ vstopvalue .or. CHR(39) $ vstopvalue && ', " probably height passed
						IF EMPTY(THISFORM.Comments.VALUE)	
							THISFORM.Comments.VALUE = vstopvalue
						ELSE
							THISFORM.Comments.VALUE = THISFORM.Comments.VALUE + " " + vstopvalue
						ENDIF

					CASE vstoploc .AND. ("/" $ vstopvalue .OR. "\" $ vstopvalue .or. (ISDIGIT(vstopvalue) .AND. AT(CHR(32),vstopvalue) > 0))
							*--Does the value have /, \, or start with a number and contain a space - assume location
							vstoploc = .F. && location found, do not check for location anymore
							*--Verify the location done in field
							THISFORM.Location.VALUE = vstopvalue
						
					CASE vstoplicensest .and. LEN(vstopvalue) = 2 .AND. VAL(vstopvalue) = 0
						*--Was a valid state entered by the user 
						IF THISFORM.StateVer(vstopvalue)	&& valid state or WM, BF, et al entered
							THISFORM.State.VALUE = vstopvalue
							vstoplicensest = .F.
						ELSE
							IF EMPTY(THISFORM.Comments.VALUE)	&& is not a valid state so put it in comments
								THISFORM.Comments.VALUE = vstopvalue
							ELSE
								THISFORM.Comments.VALUE = THISFORM.Comments.VALUE + " " + vstopvalue
							ENDIF
						ENDIF

					CASE vstoplicenseyr .and. (LEN(vstopvalue) = 4 ;
							.AND. INT(VAL(vstopvalue)) >= YEAR(DATE())-3 ;
							.AND. INT(VAL(vstopvalue)) <= YEAR(DATE())+1) && three years ago to 1 year in the future
						
						THISFORM.LicenseYear.VALUE = vstopvalue
						vstoplicenseyr = .F. && don't check for the year again


					CASE vstoplicensetag .AND. LEN(vstopvalue) <= 10 .AND. AT(CHR(32),vstopvalue) = 0 && assume license tag
							*--No spaces and less than or equal to 10 chars
							vstoplicensetag = .F. 	&& do not check for license again
							THISFORM.License.VALUE = vstopvalue
					
					OTHERWISE && assume comment after all other conditions have been checked
						*--Leftover stuff e.g. WM, BF, 62", 150lbs, 2 children in car, etc.
						IF EMPTY(THISFORM.Comments.VALUE)
							THISFORM.Comments.VALUE = vstopvalue
						ELSE
							THISFORM.Comments.VALUE = THISFORM.Comments.VALUE + " " + vstopvalue
						ENDIF
				ENDCASE
			ENDIF
		ENDFOR
	ENDIF
Any ideas?
.·*´¨)
.·`TCH
(..·*

010000110101001101101000011000010111001001110000010011110111001001000010011101010111001101110100
"When the debate is lost, slander becomes the tool of the loser." - Socrates
Vita contingit, Vive cum eo. (Life Happens, Live With it.)
"Life is not measured by the number of breaths we take, but by the moments that take our breath away." -- author unknown
"De omnibus dubitandum"
Next
Reply
Map
View

Click here to load this message in the networking platform