Understanding Foxpro IF Statement - visual-foxpro

I wasn't sure what my title should be, feel free to edit my post and change it if you can come up with something better.
There aren't many resources available on the use of Foxpro and what I'm trying to do is understand what is going on.
lldisdead=.t.
Select .f. as chkbox, * from a_counties ;
order by cn_area, cn_desc ;
into dbf (StrTmpFile1)
scan while !EOF()
IF ChkBox
selected_some_cnty = .t
endif
endscan
Here is my understanding:
Do the following as long as you are not in the last record of the
table:
IF ChkBox
Set selected_some_cnty equal to .t
Stop Check next record
Keep doing this until you are out of records.
What does IF CHKBOX mean?
Is it saying if the column CHKBOX is not null, do the following,
otherwise, do nothing.
Edit: Added additional code

If chkBox
in VFP, means:
if (chkBox)
also in all other well known languages, like C, C++, C#, Java, Go, Dart, Ruby, ... you name it - some languages parentheses are mandatory and some not. It simply mean "if chkBox is true". Sometimes you would see it written as:
If chkBox = .T.
like:
If chkBox == true
as in other languages, but it is more verbose than needed, and seasoned developers do not write it like that (after all writing like "if true is true" is awkward, simply "if true" is fine).
This is explained with comments placed in code:
* Initialize a memory variable named lldisdead as .t. (true)
lldisdead=.t.
* Select some fields into a table named m.StrTmpFile1
* StrTmpFile1 is a variable holding a string name of the table
* selecting all fields of a_counties table
* plus a boolean field named "chkBox" which is initially
* filled with .F. (false) value
Select .f. as chkbox, * from a_counties ;
order by cn_area, cn_desc ;
into dbf (StrTmpFile1)
* select's result table is table open in the current
* work area and by default located on first record.
* scanning the whole table
* with an unnecessary "while !EOF()" addition
* Default scope of scan is until EOF
scan while !EOF()
* Checking if chkBox field has a value of true
IF ChkBox
* if it has, than set "selected_some_cnty" memory variable to true
selected_some_cnty = .t
endif
endscan
Having said that, this part:
scan while !EOF()
IF ChkBox
selected_some_cnty = .t.
endif
endscan
could be written as:
scan
IF ChkBox
selected_some_cnty = .t
endif
endscan
further:
LOCATE FOR ChkBox
selected_some_cnty = !EOF()
However, since we know all chkBox values are .F., that piece of code is totally useless and could be deleted all together.

From the SQL query, the data is going into a physical table based on whatever the name "StrTmpFile1" variable is pointing to. Also note, the first column in this select statement is ".f. as ChkBox". So this is prepping EVERY RECORD in the query with a leading column that is ALWAYS False (hence .f.)
Select .f. as chkbox, * from a_counties ;
order by cn_area, cn_desc ;
into dbf (StrTmpFile1)
Now, I would suspect there is some other user interface action that is using this result table such as presenting in a grid in a form and allowing a checkbox on a column to let a user pick one or more entries to do something further.
After said selection (again, speculating intent), it is going through the loop to only find those records where the "ChkBox" COLUMN IN THE TABLE has been set to true and setting a flag as .t. that something WAS selected.
Overall, a very novice approach, but that is a different issue. A shortcut to getting the answer if a record as marked would be
select (the table)
Locate for ChkBox
selected_some_cnty = found()
Hope this helps, and if you need additional clarification, shoot a comment.

Related

How to call Lua table value explicitly when using integer counter (i,j,k) in a for loop to make the table name/address?

I have to be honest that I don't quite understand Lua that well yet. I am trying to overwrite a local numeric value assigned to a set table address (is this the right term?).
The addresses are of the type:
project.models.stor1.inputs.T_in.default, project.models.stor2.inputs.T_in.default and so on with the stor number increasing.
I would like to do this in a for loop but cannot find the right expression to make the entire string be accepted by Lua as a table address (again, I hope this is the right term).
So far, I tried the following to concatenate the strings but without success in calling and then overwriting the value:
for k = 1,10,1 do
project.models.["stor"..k].inputs.T_in.default = 25
end
for k = 1,10,1 do
"project.models.stor"..j..".T_in.default" = 25
end
EDIT:
I think I found the solution as per https://www.lua.org/pil/2.5.html:
A common mistake for beginners is to confuse a.x with a[x]. The first form represents a["x"], that is, a table indexed by the string "x". The second form is a table indexed by the value of the variable x. See the difference:
for k = 1,10,1 do
project["models"]["stor"..k]["inputs"]["T_in"]["default"] = 25
end
You were almost close.
Lua supports this representation by providing a.name as syntactic sugar for a["name"].
Read more: https://www.lua.org/pil/2.5.html
You can use only one syntax in time.
Either tbl.key or tbl["key"].
The limitation of . is that you can only use constant strings in it (which are also valid variable names).
In square brackets [] you can evaluate runtime expressions.
Correct way to do it:
project.models["stor"..k].inputs.T_in.default = 25
The . in models.["stor"..k] is unnecessary and causes an error. The correct syntax is just models["stor"..k].

AddField function does not work as expected

I have the following block of code that iterates through the fields of each table and adds the fields of the current table respectively in order to create a number of tableboxes.
'iterate through every table
For i=1 To arrTCount
'the arrFF array holds the names of the fields of each table
arrFF = Split(arrFields(i), ", ")
arrFFCount = UBound(arrFF)
'create a tablebox
Set TB = ActiveDocument.Sheets("Main").CreateTableBox
'iterate through the fields of the array
For j=0 to (arrFFCount - 1)
'add the field to the tablebox
TB.AddField arrFF(j)
'Msgbox(arrFF(j))
Next
Set tboxprop = TB.GetProperties
tboxprop.Layout.Frame.ObjectId = "TB" + CStr(i)
TB.SetProperties tboxprop
Next
The above code creates the tableboxes, but with one field less every time (the last one is missing). If I change the For loop from For j=0 To (arrFFCount - 1) to For j=0 To (arrFFCount) it creates empty tableboxes and seems to execute forever. Regarding this change, I tested the field names with the Msgbox(arrFF(j)) command and it shows me the correct field names as I want them to be in the tableboxes in the UI of QlikView.
Does anybody have an idea of what seems to be the problem here? Can I do this in a different way?
To clarify the situation here and what I have tested so far, I have 11 tables to make tableboxes of and I have tried with just one of them or some of them. The result I am seeing with the code is on the left and what I am expecting to see is on the right of the following image. Please note that the number of fields vary for each table and the image has just one of them as an example.

Mismtach Error in Foxpro SQL insertion

I need someone could help me out on how to trace the error of "mismatched data type" in visual foxpro 6.0 When I issues a command like this "insert into tmpcur from memvar".
tmpcur is a cursor having bulk numbers of columns and it is ready hard to trace which one is having mismatch in data type for insertion problem.
It is pretty difficult to trace the insertion loop of each record into VFP tables one by one unliked MSSQL profiler.
Appreciate to someone could help. Thanks.
This should help you. I have a temp cursor created with some bogus field / column names testing for types of character, integer, double, currency, date and time. Trying to follow what is the result of your scenario, I am taking the memory variable of "bbbb" which should be double (or numeric at the least), and changed it to a string.
I am then HOLDING the error trapping routine that MAY be in effect, then setting my own (as I don't think try/catch existed in VFP6.. it may, but I just don't remember. So, I did an ON ERROR, set a variable to true. Then, I default it to false, try the insert, then check the flag. If the flag IS set, then I go into a loop and try for each column in the given table/alias (in my example it is "C_Tmp", so replace with your table/alias). It goes through each variable, and if the data type is different from the table structure, it will dump the column name and table / memory value for you to review.
You could put this to a log file or something.
Now, another consideration. Some types are completely valid and common for implied conversion, such as character and memo fields can both get strings. Integer, double, float, currency can all work with generic "numeric" values.
So, if you encounter these differences, then we can go one level further and look for comparable types, but let me know and we can adjust as needed.
At least this should give you a huge jump to your insert issue.
CREATE CURSOR C_tmp ( cccc c(10), iiii i, bbbb b(2), ccyyyy y, ddd d, tttt t )
SCATTER MEMVAR memo
m.bbbb = "wrong data type, was double with 2 decimal"
lcHoldError = ON("ERROR")
ON ERROR lFailInsert = .t.
lFailInsert = .f.
INSERT INTO C_Tmp FROM memvar
IF lFailInsert
FOR lnI = 1 TO FCOUNT( "C_Tmp" )
lcTmp = FIELD( lnI, "C_Tmp" )
IF NOT TYPE( "C_Tmp." + lcTmp ) == TYPE( "m.&lcTmp" )
? "Invalid " + lcTmp + ", C_Tmp.&lcTmp, m.&lcTmp
ENDIF
ENDFOR
ENDIF
ON ERROR &lcHoldError

Write an SQR condition statement

I'm new in SQR. I need help to write a variable and use it for a condition statement. my pseudo code goes
declare $head_print
let $head_print = (select * from PS_DTR_RPT_ACCT
where RPT_TREE_NODE = 'REAL_ESTATE_EXP'
or TREE_NODE_NUM between 4600000 and 4699999)
if(head_print contain REAL_ESTATE_EXP or Account between 46000000 and 4699999)
then head_print = "REAL ESTATE";
else head_print = "Capital ESTATE";
It's not quite clear what you want so I'm making an assumption.
It seems it is if a certain value is in table PS_DTR_RPT_ACCT, then you want it to say "REAL ESTATE" otherwise say "CAPITAL ESTATE"
Now with SQR, you have to put your SQL in a begin-select block - rules are very strict - field names must be in column 1 - code underneath NOT in column 1. In the following routine, I've tried to code your pseudo code in real SQR, however, I could not test it so you may get errors - plus I don't know your field names since it just says "select *".
Begin-Report
do GetData
End-Report
Begin-Procedure GetData
! Initialize value - if no data found, query this later and set it to the "ELSE"
Let $Head_print = ''
Begin-Select
Head_Print
! Override the value from the table but only if you need to
Let $Head_Print = 'REAL ESTATE'
from PS_DTR_RPT_ACCT
Where RPT_TREE_NODE = 'REAL_ESTATE_EXP'
or TREE_NODE_NUM between 4600000 and 4699999)
End-Select
! If $Head_print is blank, then no value was found with the sql - do the ELSE part
If $Head_Print = ''
Let $Head_Print = 'Capital Estate'
End-If
End-Procedure
SQR is quite a nice finite language to learn - syntax somewhat strict, but simple as Basic with SQL. I do recommend reading the reference manual - it's downloadable from Oracle.
Feel free to ask any other questions about SQR - I get alerts if you do - sorry it took this long to answer

Replace Illegal XML characters with value from table Oracle PL\SQL

I have a requirement to check all text fields in a Database schema for any Illegal XML characters and replace them with a predefined set of acceptable values. This is to form part of a Data transformation rule, than can be called from other functions. So this function could be asked to called over a billion times on our dataset, so I need it to operate really efficiently.
i.e. & = AND ,
' = APOS
An example of what needs to be achieved by the function should be:
Update sometable set somefield = functioncall('f&re'd');
should result in
somefield having the value of ' fANDreAPOSd'
This is to carried out by a generic type PL/SQL function that takes an input of a text field and iterate through that field and replace all illegal values.
I have had a look at http://asktom.oracle.com/pls/asktom/f?p=100:11:0::NO::P11_QUESTION_ID:2612348048
http://decipherinfosys.wordpress.com/2007/11/27/removing-un-wanted-text-from-strings-in-oracle/
For some ideas, but I have my concerns over efficiency and the flexibility of these soltuions.
The way the client wants to handle the solution is to have a table configured to contain an illegal character and it's prefered replacement. The function then uses the values selected from this table to preform the replacements.
well, not exactly what you want, but consider this:
create type xmltest is object (s clob);
select XMLTYPE.createXml(xmltest('a& and ''')) from dual;
XMLTYPE.CREATEXML(XMLTEST('A&'''))
-------------------------------------------------------------------------------------
<XMLTEST>
<S>a& &apos;</S>
</XMLTEST>
However the list of predefined of XML entities is quite small, so there wouldn't be an issue replacing them with replace
If this is because of XML I'll support hal9000 - you should let Oracle do that for you. E.g. XML functions do it automatically:
SQLPLUS> set define off
SQLPLUS> select xmlelement("e", 'foo & bar <> bar "hemingway''s"') from dual;
XMLELEMENT("E",'FOO&BAR<>BAR"HEMINGWAY''S"')
--------------------------------------------------------------------------------
<e>foo & bar <> bar "hemingway&apos;s"</e>

Resources