Strange DataWindow sort with negative numeric string - sorting

I have a sortable DataWindow in PowerBuilder (both 9 and 12.6) that is giving me the collywobbles. I have a field called Category, defined as a Char(4). The form that populates this field restricts it to 4 numeric characters. Smartass tester me saw that and went "Hmm, that doesn't disallow negatives, does it?" and checked. It didn't. Then I tried sorting by that field. If I do the query in Oracle, it sorts as predicted, with the -001 at the top. If I take the DataWindow, though, which starts out sorted by Obj_No, and tell it to sort by Category instead, it sorts in the following order:
...
0009
-001
0010
...
I'm having the darnedest time coming up with a reason that would ever make sense. Anyone else?
ETA: #avk, it looks kind of like this.
IF ls_sort_cols[i] <> '-' THEN ls_sort_str = ls_sort_str+' '+ls_sort_cols[i]+' A'
IF len(ls_sort_str) > 0 THEN
ldw_win.setsort(ls_sort_str)
ldw_win.sort()
END IF
I've confirmed that the value of ls_sort_str going into the SetSort line is "category A".

It's better to code it like this:
IF len(ls_sort_cols) > 0 THEN
ldw_win.reset()
ldw_win.ImportString( ls_sort_cols)
ldw_win.setsort("category A")
ldw_win.sort()
END IF

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].

Power Query - Multiple OR statement with values

I've been doing research on this and I find a plethora of articles related to Text, but they don't seem to be working for me.
To be clear this formula works, I'm just looking to make it more efficient. My formula looks like:
if [organization_id] = 1 or [organization_id] = 2 or [organization_id] = 3 then "North" else if … where organization_id is of type "WholeNumber"
I'd like to simplify this by doing something like:
if [organization_id] in {1, 2, 3} then "North" else if …
I've tried wrapping in Parenthesis, Braces, & Brackets. Nothing seems to work. Most articles are using some form of text.replace function and mine is just a custom column.
Does MCode within Power Query have any efficiencies like this or do I have to write out each individual statement like the first line?
I've had success with the a List.Contains formulation:
List.Contains({1,2,3}, [organization_id])
The above checks if [organization_id] is in the list supplied in the first argument.
In some cases, you may not want to hardcode a list as shown above but reference a table column instead. For example,
List.Contains(TableWithDesiredIds[id_column], [organization_id])

Return results from money (ruby) with a comparison (e.g. items more than x)

Pretty simple one.
I want to do the following
Model.where("money_rate > ?", some_other_money_item)
Any ideas?
PG::UndefinedColumn: ERROR: column "money_rate" does not exist
LINE 1: SELECT "jobs".* FROM "jobs" WHERE (money_rate > '--- !ruby/...etc
Seems that you can only do a comparison using the base method, in the above case this would be money_rate_cents
So:
Model.where("money_rate_cents > ?", some_other_money_item_in_cents)
Returns an array of items where the money attribute is higher.
Will leave that in case someone else finds it useful. Or in case I'm missing something and you can do comparisons on the whole number.

Lua table.sort issues

So, having some issues getting this table to sort correctly.
Basically, table.sort thinks 10 == 1, 20 == 2, and so on. I'll post my sort code below, but I'm not sure if that has anything to do with it. Is this just an inherent issue with the table.sort algorithm in Lua?
if svKey == "q" and metalMatch == true then
table.sort(vSort.metals, function(oneMQ, twoMQ)
return oneMQ.metalQ > twoMQ.metalQ
end)
end
Values stored in vSort.metals.metalQ are strings anywhere from 1 to 3 digits long. Is there a way to make table.sort differentiate between single-, double-, and triple-digit values?
The order operators work as follows. If both arguments are numbers, then they are compared as such. Otherwise, if both arguments are strings, then their values are compared according to the current locale. You can set the locale. Strings are compared lexicographically, which is generally character by character with shorter strings before longer strings.
If you want a numeric sort, then use, well, a numeric type. This might work:
function(oneMQ, twoMQ)
return tonumber(oneMQ.metalQ) > tonumber(twoMQ.metalQ)
end
It assumes that all the metalQ values are numeric. If not, coerce to a default or provide a fallback sort order in your sort expression for non-numeric values.

Visual Basic Function Procedure

I need help with the following H.W. problem. I have done everything except the instructions I numbered. Please help!
A furniture manufacturer makes two types of furniture—chairs and sofas.
The cost per chair is $350, the cost per sofa is $925, and the sales tax rate is 5%.
Write a Visual Basic program to create an invoice form for an order.
After the data on the left side of the form are entered, the user can display an invoice in a list box by pressing the Process Order button.
The user can click on the Clear Order Form button to clear all text boxes and the list box, and can click on the Quit button to exit the program.
The invoice number consists of the capitalized first two letters of the customer’s last name, followed by the last four digits of the zip code.
The customer name is input with the last name first, followed by a comma, a space, and the first name. However, the name is displayed in the invoice in the proper order.
The generation of the invoice number and the reordering of the first and last names should be carried out by Function procedures.
Seeing as this is homework and you haven't provided any code to show what effort you have made on your own, I'm not going to provide any specific answers, but hopefully I will try to point you in the right direction.
Your first 2 numbered items look to be variations on the same theme... string manipulation. Assuming you have the customer's address information from the order form, you just need to write 2 separate function to take the parts of the name and address, take the data you need and return the value (which covers your 3rd item).
To get parts of the name and address to generate the invoice number, you need to think about using the Left() and Right() functions.
Something like:
Dim first as String, last as String, word as String
word = "Foo"
first = Left(word, 1)
last = Right(word, 1)
Debug.Print(first) 'prints "F"
Debug.Print(last) 'prints "o"
Once you get the parts you need, then you just need to worry about joining the parts together in the order you want. The concatenation operator for strings is &. So using the above example, it would go something like:
Dim concat as String
concat = first & last
Debug.Print(concat) 'prints "Fo"
Your final item, using a Function procedure to generate the desired values, is very easily google-able (is that even a word). The syntax is very simple, so here's a quick example of a common function that is not built into VB6:
Private Function IsOdd(value as Integer) As Boolean
If (value Mod 2) = 0 Then 'determines of value is an odd or even by checking
' if the value divided by 2 has a remainder or not
' (aka Mod operator)
IsOdd = False ' if remainder is 0, set IsOdd to False
Else
IsOdd = True ' otherwise set IsOdd to True
End If
End Function
Hopefully this gets you going in the right direction.

Resources