Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
IIF syntax
Message
From
15/09/2015 09:07:30
Mike Yearwood
Toronto, Ontario, Canada
 
 
To
14/09/2015 22:06:02
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Title:
Environment versions
Visual FoxPro:
VFP 9 SP2
OS:
Windows Server 2012
Network:
Windows 2008 Server
Database:
MS SQL Server
Application:
Web
Miscellaneous
Thread ID:
01624559
Message ID:
01624621
Views:
103
>>>>>>>I have always used the if .. endif clause but am wondering if the IIF could serve me here. The example in help menu of VFP does not answer my question.
>>>>>>>here is what I want to do using if ... endif
>>>>>>>
>>>>>>>
>>>>>>>if myval<7
>>>>>>>replace myval with int(myval) && I want to remove values to the right of the decimal
>>>>>>>else
>>>>>>>? 'myval exceeds 7'
>>>>>>>endif
>>>>>>>
>>>>>>>
>>>>>>>I tried this without success - is it possible ?? (or does this function only apply to expressions) or do I just have the syntax wrong.
>>>>>>>
>>>>>>>? IIF(myval<7,Replace myval with Int(myval), 'Retained myval'  )
>>>>>>>
>>>>>>
>>>>>>IIF() returns either the second or third parameter as an expression, depending on the value of the first parameter. You're attempting to return a command rather than an expression.
>>>>>>
>>>>>>You could do something like
>>>>>>
>>>>>>REPLACE MyVal WITH IIF( MyVal < 7, INT( MyVal ), MyVal ) IN MyAlias
>>>>>>
>>>>>>but that's not a great alternative. MyVal would always be REPLACEd, even with the same value, which will affect things like GETFLDSTATE( ).
>>>>>
>>>>>I'm curious Al why you added the 'in myalias' since ken's solution (which is virtually the same) works for me without it. Unless you were attempting to display the process as it occurs (which for me was not essential component to the operation.) Thanks for your input - I have a lot of places to use this new found knowledge.
>>>>
>>>>The IN clause of REPLACE was introduced to fix a subtle bug in the REPLACE command present from the earliest days of dBASE. Basically, if:
>>>>
>>>>- you have Table A SELECTed
>>>>- the record pointer of Table A happens to be at EOF( )
>>>>- you run the command: REPLACE A.MyVal WITH ...
>>>>
>>>>Then, there is no error, but the value of A.MyVal doesn't get updated (!)
>>>>
>>>>Using the IN clause with REPLACE also means you have to worry less about which work area is currently SELECTed.
>>>>
>>>>IMO using the IN clause should be mandatory with REPLACE in almost all circumstances. The only exception I've run into over the years is in generic functions where preceding or calling code sets the proper work area in advance.
>>>>
>>>>It's worth pointing out that the UPDATE-SQL command can be useful for this kind of stuff as well. If you want to update all values in a table (i.e. you're running your REPLACE within a SCAN...ENDSCAN loop), you could do something like this instead:
>>>>
>>>>UPDATE MyAlias SET ;
>>>>  MyVal = IIF( MyVal < 7, INT( MyVal ), MyVal )
>>>>
>>>>* It would arguably be nicer to do this:
>>>>UPDATE MyAlias SET ;
>>>>  MyVal = INT( MyVal ) ;
>>>>  WHERE MyVal < 7
>>>>
>>>>* Updating a single row, or a selection of rows also requires a WHERE clause, for example:
>>>>UPDATE MyAlias SET ;
>>>>  MyVal = IIF( MyVal < 7, INT( MyVal ), MyVal ) ;
>>>>  WHERE MyPrimaryKey = SomePrimaryKeyValue
>>>>
>>>>
>>>
>>>Al
>>>Sorry to pull your explanation back to my level of understanding - but in my situation I am using several open files while doing this
>>>select 1
>>>use NowFile
>>>select 2
>>>use LastFile
>>>
>>>Accordingly how would I set this using your syntax to apply only to the LastFile. I know you had cautioned me once before to use Select 0 but that ship has sailed in this case.
>>
>>If you have MyVal columns in both NowFile and LastFile, you would need to make sure you reference the one in LastFile by adding its alias:
>>
>>REPLACE MyVal WITH IIF( LastFile.MyVal < 7, INT( LastFile.MyVal ), LastFile.MyVal ) IN LastFile
>>
>>That should work regardless of which work area is currently selected, and whether it contains an open table or not.
>
>thanks al - I think it's all starting to come together now.
>
>k

Hi Karen

First things first - design. The point of databases is not to do things Row By Agonizing Row, but to think in terms of sets. It is very rare to write a replace command to affect one record. If you are doing that, to me, it suggests this is happening inside a larger loop. 1 replace commands such as this
replace myval with int(myval) for myval<7 in LastFile
is far easier to read than a
go top
do while not eof
  replace myval with iif(myval<7,int(myval),myval) in LastFile
  skip
enddo
Please note the aliases are not required except for the IN clause. The discussion of premature optimization is itself premature. An ounce of prevention trumps it.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform