×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Modify specific field of the table by FIELD()

Modify specific field of the table by FIELD()

Modify specific field of the table by FIELD()

(OP)
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.

RE: Modify specific field of the table by FIELD()

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
https://www.doschke.name

RE: Modify specific field of the table by FIELD()

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

RE: Modify specific field of the table by FIELD()

(OP)
Thank You Olaf.

Quote ((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.)


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

RE: Modify specific field of the table by FIELD()

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: How to export DBFs with Memo or Blob fields to csv
There I'm using AFIELDS and EVALUATE.

Bye. Olaf.

Olaf Doschke Software Engineering
https://www.doschke.name

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close