Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Urgent Help needed - string operations (no RegExp, pleas
Message
From
21/12/2001 19:20:14
 
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00597614
Message ID:
00597646
Views:
33
This message has been marked as a message which has helped to the initial question of the thread.
>Hi everybody,
>
>I have this peice of code:
>local lcMask,  lcDigits, lcLetters, lcAPNUniform
>lcMask = "999999X-X999999X-X999999XX-XX999999X"
>lcApn = padr(ltrim(m.tcAPN),36)
>lcDigits = "0123456789"
>lcLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ "
>lcAPNUniform = chrtran(m.lcAPN, m.lcDigits+m.lcLetters, ;
>	replicate("9", 10)+ replicate("X", 27))
>
>X in the mask can mean either digit or letter. However, if I have digit in, say, 7th position, my lcAPNUniform doesn't put X there, it puts 9, so I mark this APN as bad, while I should not. I could not think about anything except for checking each position, where X as allowed. Is there a more effective way?
>
>Thanks a lot in advance.

Nadya --

You've probably implemented your other solution by now.

As you well noted, it seems like you need to use lower level string search functions in order to accomplish what you want.

The TRANSFORM function, though, allows us to extract specific letters at specific positions, if we have a mask which has X's in the positions we want a character, and blanks where we don't.

So, we need to generate the submasks for each specific data type in the mask. The RetFormat function replaces all positions in a mask string with a space for non-matching characters, and with an X for matching characters. That function saves the time (and ensures accuracy) for setting up the various submasks needed.

The llValid assignment expression reproduces what you're already doing (seeing if the mask and converted string are equal) with one significant exception. Because we're using a mask, rather than comparing literals, we can supply multiple "filters" through CHRTRAN, to eliminate any variation of the characters we need.

I tried this out and it gave the results that I think you're expecting.

Merry Christmas!

Jay
*	Returns a string of X's, to be used as a mask in a TRANSFORM statement
*	to return X's in the position of the passed symbol in the passed mask.


FUNCTION	RetFormat
LPARAMETER		tcMask, tcSymbol
LOCAL			lcFormat

#DEFINE   SYMBOL_NOT_IN_STRING    "~"


	lcFormat = CHRTRAN (tcMask, CHRTRAN (tcMask, tcSymbol, SYMBOL_NOT_IN_STRING), SPACE (LEN (tcMask)))
	lcFormat = STRTRAN (lcFormat, tcSymbol, "X")

	RETURN	lcFormat
ENDFUNC

PROCEDURE	SetupPatternMatch
LOCAL	lcDigits, lcLetters, lcMask, lcANMask, lcNMask, lcSepMask, lcPattern, lcValid, ;
	lcSep, lcSepBlank, llValid

	lcDigits = "0123456789"
	lcDigitsBlank = SPACE (10)
	lcLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ "
	lcLettersBlank = SPACE (27)
	lcSep = "-"
	lcSepBlank = SPACE (1)

	lcMask = "X9-9X"
	lcANMask = RetFormat (lcMask, "X")
	lcNMask = RetFormat (lcMask, "9")
	lcSepMask = RetFormat (lcMask, "-")

	lcPattern = "A8-7B"    &&  .T.
*	lcPattern = "A8-BB"    &&  .F.
*	lcPattern = "A8-77"    &&  .T. !!!


	llValid = EMPTY (CHRTRAN (TRANSFORM (lcPattern, lcNMask), lcDigits, lcDigitsBlank)) ;
		AND EMPTY (CHRTRAN (TRANSFORM (lcPattern, lcSepMask), lcSep, lcSepBlank)) ;
		AND (EMPTY (CHRTRAN (CHRTRAN (TRANSFORM (lcPattern, lcANMask), lcLetters, lcLettersBlank),  ;
			lcDigits, lcDigitsBlank)))
	WAIT WINDOW TRANSFORM (llValid)

ENDPROC
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform