Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parsing a string

Status
Not open for further replies.

txgeekgirl1

Programmer
Sep 10, 2009
85
US
I have a returned string from a black box function that looks like this:

FirstName LastName "on" DATE "Space" TIME

The variables I have listed are not real variables - just the name of what is in this string.

I need to parse to "on" to grab the name and then some kind of substr from "on" to the " " to grab the date.

Anyone know the correct STR calls?
 
Hi

Not that hard.

Use AT() to find the location of the "on" then the name should be everything to the left of that (trimmed)

Code:
m.InputString = 'FirstName LastName "on" DATE "Space" TIME '

m.FullName = Trim(left(m.InputString,At('"on"',m.InputString)-1))

To get the Date that falls between the "on" and the "Space" you would do this:

Code:
m.InputString = 'FirstName LastName "on" DATE "Space" TIME '

m.DateString = AllTrim(Substr(m.InputString,At('"on"',m.InputString)+4,At('"Space"',m.InputString)-(At('"on"',m.InputString)+4)))


Note that the "on" and "Space" have to be enclosed in single quotes



Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.
 
I may have misunderstood your query on first reading thing that the second delimiter was "Space" when perhaps you mean " "

Code:
m.InputString = 'FirstName LastName "on" DATE " " TIME '

m.DateString = AllTrim(Substr(m.InputString,At('"on"',m.InputString)+4,255)) && everything to the right of "on"
m.DateString = AllTrim(LEFT(m.DateString,At('" "',m.DateString)-1))

Taking this to the limits and assuming that you didn't really expect the "on" and "Space" to be in quotes but some kind of symbolic - you would need to find the "on" in reverse (as someone might be called "on frankel" or "frank on"

Code:
m.InputString = 'FirstName LastName on DATE TIME '

m.DateString = AllTrim(Substr(m.InputString,At(' on ',m.InputString)+4,255)) && everything to the right of " on "
m.DateString = AllTrim(LEFT(m.DateString,At(' ',m.DateString)-1))

? m.datestring

m.FullName = Trim(left(m.InputString,Rat(' on ',m.InputString)-1))

? m.FullName


Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.
 
Thanks - you all have gotten me closer to pargsing this electronic signature string.... Everything but the date so far. I have tried everything suggested with the date this actually gives me a 0. If I play with the +4 in the original suggestion and make it a 10 I get the "2" for 2010.
Code:
SELECT *, ; 	
TRIM(LEFT(rptsiglinex2,AT(",",rptsiglinex2)-1)) AS RptSigName, ;
	ALLTRIM(SUBSTR(rptsiglinex2,AT(" on ",rptsiglinex2),255)) AS RptSigDate, ;
	LEFT(RIGHT(rptsiglinex2,10),8) AS RptSigTime ;
	FROM MyTemp_ ;
 
Ah, with the benefit of rereading...

You should use the last occurance of ' on ' for the date too!
Code:
m.InputString = 'FirstName LastName on DATE TIME '
m.DateString = AllTrim(Substr(m.InputString,Rat(' on ',m.InputString)+4,255)) && everything to the right of " on "
m.DateString = AllTrim(LEFT(m.DateString,At(' ',m.DateString)-1))
? m.datestring

m.FullName = Trim(left(m.InputString,Rat(' on ',m.InputString)-1))
? m.FullName

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.
 
Ah,

Code:
SELECT *, ;
     TRIM(LEFT(rptsiglinex2,AT(",",rptsiglinex2)-1)) AS RptSigName, ;
    ALLTRIM(SUBSTR(rptsiglinex2,AT(" on ",rptsiglinex2),10)) AS RptSigDate, ;
    RIGHT(Trim(rptsiglinex2),8) AS RptSigTime ;
    FROM MyTemp_ ;

The date is PROBABLY 10 characters long (10/12/2010)...

If you want the time, you need to strip the string of any trailing spaces and you only need the last 8 or 11 chars (00:00:00 or 00:00:00:00)

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.
 
If it were a single string you might use something like this:

Code:
cName     = TRIM(STREXTRACT(m.lcStr,'',' on '))
cDatetime = ALLTRIM(STREXTRACT(m.lcStr,' on '))

If it is many then probably something like this would be better:
Code:
StrToFile(STRTRAN(m.lcString,' on ', '|',1,-1,1),'tmp.txt')
create cursor myDataParser (cName c(250), dtime  c(20))
append from tmp.txt delimited with "" with character "|"
erase ('tmp.txt')



Cetin Basoz
MS Foxpro MVP, MCP
 
[ ]
[TT][BLUE]zstr = "Joe Blow on 2/4/2010 12:13:34"

zname = ATXLEFT(zstr, " on")
zdate = ATXRIGHT(zstr, " on ", " ")[/BLUE][/TT]


See faq184-5975 for code for ATXLEFT and ATXRIGHT.

mmerlinn


"We've found by experience that people who are careless and sloppy writers are usually also careless and sloppy at thinking and coding. Answering questions for careless and sloppy thinkers is not rewarding." - Eric Raymond

Poor people do not hire employees. If you soak the rich, who are you going to work for?
 
[ ]

If both parts of the string are separated by " on " you could do:

[tt][blue]zstr = "Joe Blow on 2/4/2010 on 12:13:34"

zname = ATXLEFT(zstr, " on")
zdate = ATXRIGHT(zstr, " on ", " on ")[/blue][/tt]


Note that using these UDFs that the LENGTH of any part of the string does not matter. It will always return the correct part of the string regardless of how long that specific part is.

For example, dates typically can be anywhere from 6 characters to 10 characters long. This will return whatever happens to be there. This could be important if all of your dates are not the same length.

See FAQ184-5975: ATXLEFT() & ATXRIGHT(): Two useful UDFs for parsing strings. for code for ATXLEFT and ATXRIGHT.

mmerlinn


"We've found by experience that people who are careless and sloppy writers are usually also careless and sloppy at thinking and coding. Answering questions for careless and sloppy thinkers is not rewarding." - Eric Raymond

Poor people do not hire employees. If you soak the rich, who are you going to work for?
 
Yep - I think the issue is this - Some of the functions you all are suggesting are not offered in my version.

State agencies often don't have the luxury of upgrading due to original language and coding conflicts. Although we are investigating a platform change - what I have to work with is VFP6.0 and functions like STREXTRACT and ATXRIGHT/ATXLEFT aren't included.

I even spent time with my boss this morning on this issue and he was flabbergasted. Takes a lot to stump him. Think I am going to try and peel off the the last 30 and then go after it that way.... kind of what I did with the TIME. TIME was easy to peel as I knew it was the last 10 characters - the last 2.
 
Sharing what I came up with as solution - totally bites because it's sloppy but works.

Code:
*Sloppy way to get the date but it works..... Do not mess with this even though it's inefficient - lag time is seconds.
SELECT *, ;
	SUBSTR(rptsiglinex,37,75) AS rptsiglinex2, ;
	RIGHT(rptsiglinex,21) AS GoAfterDate ;
FROM esigdis1_ ;
INTO CURSOR MyTemp_
		
*Dissect and feed into RPT variables.  
SELECT *, ;
	TRIM(LEFT(rptsiglinex2,AT(",",rptsiglinex2)-1)) AS RptSigName, ;
	LEFT(GoAfterDate,10) AS RptSigDate, ;
	LEFT(RIGHT(rptsiglinex2,10),8) AS RptSigTime, ;
	IIF(CTOD(LEFT(GoAfterDate,10)) = SvcDate, "Concurrent    ","Not Concurrent") AS FLAG ;
	FROM MyTemp_ ;
	INTO CURSOR esigdis_
 
[ ]
The most important thing is that you have finally got a solution that works for you. Congratulations. I hope that we were able to help you in some small way to accomplish that.

what I have to work with is VFP6.0 and functions like STREXTRACT and ATXRIGHT/ATXLEFT aren't included.

ATXRIGHT/ATXLEFT are not included in ANY version of VFP. They are USER DEFINED FUNCTIONS that you incorporate into your code and call like you would call any other function.

UDFs of any kind can be used in any version of VFP that supports the code included within the UDF. In this case, the ATXRIGHT/ATXLEFT UDFs will work in ANY version of VFP, even your version which is VFP6.0.

That is not to say they are the best solution, just that they are one of MANY possible solutions to your problem. The real advantage I see with them is that you do not need to know the numerical location of anything in your string nor do you need to determine that location by using AT() or RAT(). All you need to know is what characters define the breakpoints within the string that you need to parse.

In your case " on " is the group of characters defining your data. Because of the differing lengths of names, you have no idea exactly where that group lands within the string. Since STREXTRACT is not available to you, AT() and RAT() are typically used to find the location of " on ". ATXRIGHT/ATXLEFT are an attempt to make such string parsing easier when tools like STREXTRACT are not available.

mmerlinn


"We've found by experience that people who are careless and sloppy writers are usually also careless and sloppy at thinking and coding. Answering questions for careless and sloppy thinkers is not rewarding." - Eric Raymond

Poor people do not hire employees. If you soak the rich, who are you going to work for?
 
Txgeekgirl1,

Glad to see you've got it working. I hadn't realised you were on VFP 6.0, otherwise I wouldn't have suggested STREXTRACT().

But there's nothing wrong with your solution.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro tips, advice, training, consultancy
Custom software for your business
 
I just want to thank you all - sometimes just bouncing ideas gives just what you need to figure out what will and won't work.

 
another one:

Code:
lcStr = "Joe Blow on 2/4/2010 12:13:34"
lcStr = Strtran(lcStr," on ","|")
lcName     = GetWordnum(lcStr,1,"|")
lcDateTime = GetWordnum(lcStr,2,"|")

lcDate = GetWordnum(lcDateTime,1)
lcTime = GetWordNum(lcDateTime,2)
? lcName, lcDate, lcTime
? CTOT(lcDateTime) && depends on DATE settings to work

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top