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

Finding 2 sets of numbers and transferring each to separate field 2

Status
Not open for further replies.

Phil Thoms

Programmer
Oct 31, 2005
245
GB
Hello,
I wish to find the value of 2 numbers in a text field of variable length. These numbers appear in brackets at the end of the text:-
xxxxxxxxxxxxxx (2,9.5mm)
xxxxxxxxxxxxxxxxxxxxxxxx(10,10mm)

The first figure can be 2 chars in length and the last figure can be whole or with decimal of .5
Once found I wish to transfer each to a field of their own (mm does not need to be transferred).

Thanks for your help.
 
Assuming your table / cursor is named cdemo, and the text field is named ctextfield, here is a slightly changed version :

Code:
LOCAL lnNewValue1, lnNewValue2
** The only purpose of this part is to have some testing data
* Creating a cursor
create cursor cdemo (ctextfield C(50))
* Populate the cursor with some values
insert into cdemo values('xxxxxxxxxxxxxx (2,9.5mm)')
insert into cdemo values('xxxxxxxxxxxxxxxxxxxxxxxx(10,10mm)')
insert into cdemo values('another value (in cm this time) (19,3.5cm)')

** in your code you will change "cdemo" with the name of your table and  "cdemo.ctextfield" with the appropriate names (tablename.fieldname)
select cdemo && here
scan && scan the cursor
	getvalues(cdemo.ctextfield, @lnNewValue1, @lnNewValue2) && and here
	?m.lnNewValue1, m.lnNewValue2
endscan

PROCEDURE getvalues
	LPARAMETERS  lcOldValue, lnNewValue1, lnNewValue2
	LOCAL ln1, ln2, ln3
	ln1 = RAT("(", m.lcOldValue) + 1 && I also replaced AT() with RAT()
	ln2 = RAT(",", m.lcOldValue) 
	lnNewValue1 = VAL(SUBSTR(m.lcOldValue, m.ln1, m.ln2 - m.ln1))
	ln3 = RAT(")", m.lcOldValue) - 1
	lnNewValue2 = VAL(SUBSTR(m.lcOldValue, m.ln2 + 1, m.ln3 - m.ln2))
RETURN

Respectfully,
Vilhelm-Ion Praisach
Resita, Romania
 
Your step 2 comes up with an error 'Function name is required'.
So you're saying VFP6 doesn't know RAT or SUBSTR? Or is it you didn't changed text to whatever field name you need to process?
You are not very informative, so we could write could you could simply grab as is.

I have worked with VFP6 and am pretty sure both RAT and SUBSTR are defined in the language at that version. Are you sure you are at VFP6? What is ?Version() showing?

Bye, Olaf.

 
In Olaf Doschke's code, try these changes :

Step 0: use the foxtools library
Try with a fool path.
In my case (VFP9)
instead of :
SET LIBRARY TO foxtools.fll
I used :
SET LIBRARY TO "C:\Program Files (x86)\Microsoft Visual FoxPro 9\foxtools.fll"

Step 1: extract paranthesis/bracket at the end, trailing chars not removed.
Here, text must be repalced with the name of your table filed
lcValues = SUBSTR(text,RAT("(",text)+1)


Step 2: Separate values
Here is missing a closing bracket
lnVal1 = Val(Wordnum(lcValues,1,",") ) && add the second ")"
lnVal2 = Val(Wordnum(lcValues,2,",") ) && add the second ")"

Respectfully,
Vilhelm-Ion Praisach
Resita, Romania
 
I have worked with VFP6 and am pretty sure both RAT and SUBSTR are defined in the language at that version.

Yes, of course that's right. In fact,SUBSTR() goes all the way back to FoxBase and dBASE.

Mike



__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Ouch, step 2 is the last step. Should have added the Step 0....Yes, it simply misses a closing bracket.
Hard to see, isn't it?

Bye, Olaf.
 
Hi vguliemus,
Thanks again, will try the new way and let you know.

 
Hello Olaf,
Sorry if I mislead. Brackets were missing from step 2.

Regards
 
>Sorry if I mislead.
No, that was I myself. It's jsut, if you didn't see the missing bracket, I also didn't.
Plus I tricked myself in counting from zero.

Bye, Olaf.
 
Hi vguliemus,
Thanks for your latest code. I already have a table for testing purposes, so I assume I use the replace command within the scan/endscan scope
instead of the ? command, unless there is a better way.

Regards.
 
Indeed, you must use the REPLACE command, instead of the ? command.
Well, in Foxpro, and, generally speaking, in any programming language, almost always are alternative ways to accomplish a particular task.
In this thread, you got at least one of them, for your request.

Respectfully,
Vilhelm-Ion Praisach
Resita, Romania
 
Hello vgulielmus,
I am using your latest code with a table of 20 records. I've inserted the following within the scan scope:-
repl size with lnNewValue1, qty with lnNewValue2

It is replacing the 2 fields with zero's

I assume my code is correct.

Regards
 
A few possibilities :

- because lnNewValue1 and lnNewValue2 are output parameters for getvalues(), they must be passed by reference; maybe you forgot the "@"character
getvalues(cdemo.ctextfield, @lnNewValue1, @lnNewValue2)

- your text field has additional characters after the pair of numbers, like
xxxxxxxxxxxxxx (2,9.5mm) (some additional characters here)

- the text field belongs to one table, while size and qty to another

But these are only suppositions.


Respectfully,
Vilhelm-Ion Praisach
Resita, Romania
 
Hello vgulielmus,
The following is the code being used:- I have assumed that you remove the @ symbol (doesn't work if they are included)

LOCAL lnNewValue1, lnNewValue2
scan && scan the cursor
getvalues(etemp.item, @lnNewValue1, @lnNewValue2) && and here
repl size with lnNewValue1, qty with lnNewValue2
endscan

PROCEDURE getvalues
LPARAMETERS lcOldValue, lnNewValue1, lnNewValue2
LOCAL ln1, ln2, ln3
ln1 = RAT("(", m.lcOldValue) + 1 && I also replaced AT() with RAT()
ln2 = RAT(",", m.lcOldValue)
lnNewValue1 = VAL(SUBSTR(m.lcOldValue, m.ln1, m.ln2 - m.ln1))
ln3 = RAT(")", m.lcOldValue) - 1
lnNewValue2 = VAL(SUBSTR(m.lcOldValue, m.ln2 + 1, m.ln3 - m.ln2))
RETURN

Thanks
 
The @ are absolutely necessary. No function can return two values, you can only make parameters passed by reference via @, also in VFP6. If not Set Udfparms To REFERENCE.
If not, you don't get the single values parsed from the getvalues procedure.

Another thing: I'd judge 10mm a size (length) and 10 a quantity. So value1 is qty and value2 is size, not vice versa.

Bye, Olaf.
 
Hello Olaf,
I have just realised that the type of brackets used in the text field are [10mm,5] NOT the usual type ().
My fault for not looking properly.
Both sets of code now work perfectly.
Many thanks to all.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top