suppressing printing every assignment - octave

I have written a simple script in Octave. When I run it from the command line, Octave prints a line every time a variable gets assigned a new value. How do I suppress that?
MWE:
function result = stuff()
result = 0
for i=0:10,
j += i
end
end
when I run it:
octave:17> stuff()
result = 0
result = 0
result = 1
result = 3
result = 6
result = 10
result = 15
result = 21
result = 28
result = 36
result = 45
result = 55
ans = 55
octave:18>
I want to get rid of the result = ... lines.
I am new to Octave, so please forgive me asking such a basic question.

by adding a semicolon at the end of your statement it will suppress the intermediate result.
In your case:
function result = stuff()
result = 0;
for i=0:10,
j += i;
end
end
will do the trick.

Like in matlab just add a ; (semicolon) to the end of a line you don't want output to the terminal.

Related

Sharing variables between functions? (Python3)

Here is the code I have written in Python:
def checkout_item(item):
quant = int(input('Enter the quantity of the item purchased: '))
price = float(input('Enter price of item: $'))
item_total = quant*price*1.00
print(f'Your item total is: ${item_total}')
return item_total
def main():
count = 1
order_total = 00.00
flageus_maximus = True
while flageus_maximus:
order_total+=checkout_item(count)
count+=1
print(f'Item no: {count}')
keep_going = input('Do you have more items to checkout?')
if keep_going!='yes':
flageus_maximus=False
print(f'Your order total is ${order_total}')
main()
I want to print 'Item no:' before item_total, but in order to do that, I have to readjust my checkout_item function. I was thinking of returning the count variable through the checkout_item(item) function into the mainline, but the checkout_item(item) function already returns item_total. What can I do?
I tried to set a count variable in checkout_item(item), by adding a variable count_checkout = 0 in checkout_item(item), but I got a message stating count_checkout: Unbound. I'm guessing it is because I cannot return two variables from checkout_item(item) as I call it in the main()

MS Access DLookup Multiple lookup Runtime error 13 type mismatch

I have what I think is a simple problem, but I must be missing something.
This line of VBA works:
If Me.To = 340 And DLookup("[LocID]", "[qryMyLocation]") = "C25" Then
However, I need to test against 2 LocID's and this doesn't work:
If Me.To = 340 And DLookup("[LocID]", "[qryMyLocation]") = "C25" Or "C20" Then
What am I missing - is the above possible?
I receive the error when trying the 2nd line of code: Runtime error 13 type mismatch
It's because you need to write the comparaison again, VBA doesn't understand "C20" as a valid boolean expression.
If Me.To = 340 And ((DLookup("[LocID]", "[qryMyLocation]") = "C25" Or DLookup("[LocID]", "[qryMyLocation]") = "C20")) Then
To get faster results, you can store the DLookup into a variable.
Dim LocID as Variant 'Put the right type here, I think it should be String
LocID = DLookup("[LocID]", "[qryMyLocation]")
If Me.To = 340 And (LocID = "C25" Or LocID = "C20") Then
Edit : I added the parenthesis the same as the accepted answer because of operator precedence. Leaving the parenthesis out would result in
If (Me.To = 340 And LocID = "C25") Or LocID = "C20" Then
You were close. Try this:
If Me.To = 340 And (DLookup("[LocID]", "[qryMyLocation]") = "C25" Or DLookup("[LocID]", "[qryMyLocation]") = "C20") Then
Note that I added some parenthesis, too.
A lookup only returns one result, but you can add your desired results in the criteria and count the returned results.
If Me.To = 340 And DCount("[LocID]", "[qryMyLocation]", "[LocID] = 'C25' Or [LocID] = 'C20'") > 0 Then

Scilab - for loop - finding matching data points - from matrices of different lengths - index error

Within Scilab I am trying to find data points (time) that match then index these points (a1 continous, a2 discreet events). I can take this index to select data points from other data sets so then I can analyse data based on the discreet events (a2).
The below code gives me an 'index error' on this line 'if a1(i) == a2(j);'
a1 = [1,2,3,4,5,6,7,8,9,10,11,12,13]
a2 = [3,4,6,8,10,12]
x = 0
for i = x:length(a1);
for j = 0:length(a2);
if a1(i) == a2(j);
disp(x)
end
end
end
If there are any proficient Scilab users out there to help, it would be much appreciated.
Please look at the intersect function. It does exactly what you want in an efficient way
I discovered the problem. The invalid index was due to the matrix not having a zero index.
a1 = [1,2,3,4,5,6,7,8,9,10,11,12,13]
a2 = [3,4,6,8,10,12]
x = 1;
for i = 1:13;
for j = x:6;
if a1(i) == a2(j);
disp(a2(j))
x = j
end
end
end

MySQL user-defined function returns incorrect value when used in a SELECT statement

I met a problem when calling a user-defined function in MySQL. The computation is very simple but can't grasp where it went wrong and why it went wrong. Here's the thing.
So I created this function:
DELIMITER //
CREATE FUNCTION fn_computeLoanAmortization (_empId INT, _typeId INT)
RETURNS DECIMAL(17, 2)
BEGIN
SET #loanDeduction = 0.00;
SELECT TotalAmount, PeriodicDeduction, TotalInstallments, DeductionFlag
INTO #totalAmount, #periodicDeduction, #totalInstallments, #deductionFlag
FROM loans_table
WHERE TypeId = _typeId AND EmpId = _empId;
IF (#deductionFlag = 1) THEN
SET #remaining = #totalAmount - #totalInstallments;
IF(#remaining < #periodicDeduction) THEN
SET #loanDeduction = #remaining;
ELSE
SET #loanDeduction = #periodicDeduction;
END IF;
END IF;
RETURN #loanDeduction;
END;//
DELIMITER ;
If I call it like this, it works fine:
SELECT fn_computeLoanAmortization(3, 4)
But if I call it inside a SELECT statement, the result becomes erroneous:
SELECT Id, fn_computeLoanAmortization(Id, 4) AS Amort FROM emp_table
There's only one entry in the loans_table and the above statement should only result with one row having value in the Amort column but there are lots of random rows with the same Amort value as the one with the matching entry, which should not be the case.
Have anyone met this kind of weird dilemma? Or I might have done something wrong from my end. Kindly enlighten me.
Thank you very much.
EDIT:
By erroneous, I meant it like this:
loans_table has one record
EmpId = 1
TypeId = 2
PeriodicDeduction = 100
TotalAmount = 1000
TotalInstallments = 200
DeductionFlag = 1
emp_table has several rows
EmpId = 1
Name = Paolo
EmpId = 2
Name = Nikko
...
EmpId = 5
Name = Ariel
when I query the following statements, I get the correct value:
SELECT fn_computeLoanAmortization(1, 2)
SELECT Id, fn_computeLoanAmortization(Id, 2) AS Amort FROM emp_table WHERE EmpId = 1
But when I query this statement, I get incorrect values:
SELECT Id, fn_computeLoanAmortization(Id, 2) AS Amort FROM emp_table
Resultset would be:
EmpId | Amort
--------------------
1 | 100
2 | 100 (this should be 0, but the query returns 100)
3 | 100 (same error here)
...
5 | 100 (same error here up to the last record)
Inside your function, the variables you use to retrieve the values from the loans_table table are not local variables local to the function but session variables. When the select inside the function does not find any row, those variables still have the same values as from the previous execution of the function.
Use real local variables instead. In order to do that, use the variables names without # as a prefix and declare the variables at the beginning of the function. See this answer for more details.
I suspect the problem is that the variables in the INTO are not re-set when there is no matching row.
Just set them before the INTO:
BEGIN
SET #loanDeduction = 0.00;
SET #totalAmount = 0;
SET #periodicDeduction = 0;
SET #totalInstallments = 0;
SET #deductionFlag = 0;
SELECT TotalAmount, PeriodicDeduction, TotalInstallments, DeductionFlag
. . .
You might just want to set them to NULL.
Or, switch your logic to use local variables:
SET v_loanDeduction = 0.00;
SET v_totalAmount = 0;
SET v_periodicDeduction = 0;
SET v_totalInstallments = 0;
SET v_deductionFlag = 0;
And so on.

SQL BINARY Operator not showing exact match

I have a table with the following columns...
[Name] = [Transliteration] = [Hexadecimal] = [HexadecimalUTF8]
...with multiple rows of UTF-8 characters, such as:
ङ = ṅa = 0919 = e0a499
ञ = ña = 091e = e0a49e
ण = ṇa = 0923 = e0a4a3
न = na = 0928 = e0a4a8
In order to search for the row that exactly matches ña in the Transliteration column , I enter the following command:
SELECT DISTINCT * FROM Samskrta
WHERE BINARY (Transliteration = concat(0xc3b161))
ORDER BY HexadecimalUTF8;
...which produces 4 rows.
Why is the SQL command not producing only the row that exactly matches ña?
What SQL command produces only the row that exactly matches ña?
The following command produces the same results:
SELECT DISTINCT * FROM Samskrta
WHERE BINARY (Transliteration = 'ña')
ORDER BY HexadecimalUTF8;
FIRST OF ALL, your query can't work as indicated: you are applying BINARY() to the result of the logical comparison, NOT comparing the BINARY() of whatever to whatever.
Try reproducing your code PRECISELY if you expect people to be able to tender assistance.