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!

Modify specific field of the table by FIELD()

Status
Not open for further replies.

Noob_Coder

Programmer
Feb 18, 2020
2
CZ
Hi all experience Foxies,

I have table named table1 which has two colums named col1 and col2.
I want set field of the table1 on position [3,2] by string value "test".

I now use:

Code:
GOTO 3
myfield = FIELD(2)
replace &myfield WITH "test"

It works, but it is not so clear as opposite process, getting value from field of the table,
where I use:

Code:
GOTO 3
i = EVALUATE(FIELD(2))

Is there any better and cleary way how to do It?

I need access to cols just by index not by columname.

So my second question is:
How to use FIELD() to get same effect like

Code:
GOTO 3
replace col2 WITH "test"

I use VFP9.

Thank You all for your hints and tips.
 
Just to shortcut this: If you want to address queried data as an array, you could SELECT ... INTO ARRAY yourarray.

Seems you want to address some code that used a recordset to work on a cursor now, but that's not really fortunate, not really performant.

Anyway, you could use the concept of name expression:

Code:
Create Cursor crsTest (col1 c(10), col2 c(10), col3 c(10))
Insert into crsTest VALUES ("one","two","three")
Insert into crsTest VALUES ("four","five","six")
Insert into crsTest VALUES ("seven","eight","nine")

Goto 3 in crsTest
? Field(2), " is ", eval(Field(2))
Replace (Field(2)) with "test" in crsTest

Browse
(Field(2)) specified in parenthesis is making it a name expression. Without it you get an error about "unrecognized phrase/keyword", REPLACE expects a column name here, a name expression gives REPLACE all it needs at that place, telling it to parse the value within the brackets as a name, not as the literal value it is.

You just have to realize what makes a name expression valid: The command you use has to expect a name at the place you use it, that's also true for file names in the COPY FILE command, for example, so it's not just about table or column names. So why do you need eval for the ? command? This prints any expression and so won't expect a name expression, even though you can surely print a column by its name. The brackets become obsolete or only necessary in the mathematical sense to compute the content in them first, like ? (1+2)*3 vs ? 1+2*3.

Bye, Olaf.

Olaf Doschke Software Engineering
 
Either of your two options - &myfield and EVALUATE() - will work. But if your aim is to make your code clearer and easier to understand, you are out of luck. That is the inevitable consequence of addressing fields by position rather than by name.

The founding fathers gave us field names so that we can make our code readable. If we discard those names, we must pay the price in clarity and readability.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Thank You Olaf.


This trick with parenthesis helped me. It works perfectly
Code is now cleary..

Code:
GOTO 3
REPLACE (FIELD(2)) WITH "test"

insteed

Code:
GOTO 3
myfield = FIELD(2)
REPLACE &myfield WITH "test"

It is part of my program to convert table data to csv file. I have empty table with two cols for name and surname and ten cols for emails. I generate database, which I use for testing of the program.

Code:
* Test database
FOR i = 1 TO 20
	APPEND BLANK
	REPLACE col1 WITH "name" + ALLTRIM(STR(i))
	REPLACE col2 WITH "surname" + ALLTRIM(STR(i))
	
	randval = INT(RAND(-1)*10) + 1	&& random <1,10>, I think, It doesn't work so well... 
	
	FOR j = 1 TO randval
		*myfield = FIELD(2+j)
		*REPLACE &myfield WITH "email" + ALLTRIM(STR(i)) + "@email" + ALLTRIM(STR(j)) + ".cz"
		REPLACE (FIELD(2+j)) WITH "email" + ALLTRIM(STR(i)) + "@email" + ALLTRIM(STR(j)) + ".cz"	
	ENDFOR 
ENDFOR

But now, when I want to write content of table field to VPF console, I have problem.

For Example I want write content on [3,2] position

Code:
GOTO 3
? FIELD(2)		&& It doesnt work
? (FIELD(2))		&& It doesnt work too!
? EVAL(FIELD(2))	&& It works...
? col2			&& It works

This is why I used EVAL.
Why second variant doesn't work? Is EVAL truly need here?

Thank You
 
I explained why you need EVAL in the ? command already.

Brackets are simply brackets when no name is expected by a command. The ? command expects literal values, expressions, anything, noithing specific. REPLACE expects a name of a column.
Putting anything in brackets does not automatically turn it into a name expression.

Besides, to convert data into CSV generally has this FAQ:faq184-7770
There I'm using AFIELDS and EVALUATE.

Bye. Olaf.

Olaf Doschke Software Engineering
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top