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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replace a placeholder with a variable

Status
Not open for further replies.

VFP2013

Programmer
Oct 19, 2013
20
US
Hi, sorry for the numerous posts. This time I couldn't figure out how to replace a placeholder (such as %P for customer) with a variable string (such as the customer's first name from the accounts database). Here's what I'm doing, I'm making it to where the customer's can be emailed/texted via an external program to mine that gathers text files and sends the contents. I want the message to be customizable, so say the message is "Hello, %P. We just wanted to inform you that order # %O is ready" So, to put the strings into the text files the placeholders, %P & %O, would need to say the customer's name and order #.
Thanks,
Ty G.
 
Helly Ty,

If your text is in a variable lcText, eg by

lcText = mailtable.mailbody
lcName = persons.Name

You could use lcText = STRTRAN(lcText,"%P", lcName)

You could make use of another feature of VFP called text merging.
Code:
Select customer.name as RecipientName, ;
order.Ordernumber, ;
customer.mail as RecipientMail ;
From Order;
Left join customer on Order.customerid = customer.id;
Where Order.state = "ready" and order.mailsent = .F.;
Into Cursor OrderReadyRecipients

Select OrderReadyRecipients
Scan
TEXT TO lcText TEXTMERGE
Hello, <<OrderReadyRecipients.RecipientName>>

We just wanted to inform you that order # <<OrderReadyRecipients.OrderNumber>> is ready.
ENDTEXT
* Sendmail(OrderReadyRecipients.RecipientMail, lcText)
Endscan

You can also use code expressions, eg <<DATE()>> for todays date.

That'll replace <<OrderReadyRecipients.RecipientName>> with the value of the field OrderReadyRecipients.RecipientName, which should of course exist in the table or cursor OrderReadyRecipients. If you send such mails, you will have a table of recipients you want to loop.

The downside of this is, if users should specify the template text, they would need to specify exactly the right technical expressions, table and field names they won't know. You can stay with %P and %O and other placeholders and could replace them with the appropriate technical <<expression>> first and then use that modified placeholders to fill them with variables, table fields etc. Instead of TEXT..ENDTEXT as given above, you also have a Textmerge() function, which of course is best used, if the template text should not be hardcoded into your program, but should come from a mailbody memo field, for example.

Bye, Olaf.
 
Ty,

Of the options that Olaf suggested, I'd go with STRTRAN(). It's simple and straightforward, and is clearly explained in the Help.

Keep in mind that, by default, STRTRAN() converts every instance of your place-holder. So, if "%P" occurs three times, the function will replace all three instances. That's probably what you want. If it isn't, you can pass parameters that specify the first occurrence to replace and the number of occurrences.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Yes, Olaf, I could use the <<>> tags, but this program is designed for a dry cleaner's shop in my town whom I am writing this program for to get him off a DOS FoxPro POS system. So, his employees don't know much about that kind of stuff which is why it would be simpler to use %placeholder. But, I did decide to go with STRTRAN()
 
That's fine.

But I think nobody got my idea to combine the advantages:

1. %placeholder is easier to understand, so yes, use it
2. <<textmerge>> is easier to process with variables, fields, in loops. It replaces all in 1 single call

So let the end user enter &placeholder, then use STRTRAN to replace the %placeholders with <<placeholders>> and then loop over data and use TEXTMERGE.

Sounds too complicated? If you strtran %placeholder you can insert the end value right away, sure, but creating an intermediate expression for textmerge can be used multiple times for a list of records.

Some numbers, your short text has 2 placeholders:
Hello, %P. We just wanted to inform you that order # %O is ready

Say you have 1000 recipients, that's 2000 STRTRANs.
Or you do 2 STRTRAN and then 1000 TEXTMERGES, that's 1002 vs 2000 function calls with comparable complexity, both STRTRAN and TEXTMERGE do process a string once from begin to end.

Bye, Olaf.
 
Well, the send-outs are done as the orders are marked ready. orders are marked one-by-one
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top