How to perform mod(%) operation in smarty? - smarty

I'm a newbie to Smarty. I want to do a mod operation on an array element in smarty. Following is the code snippet I'm trying to implement:
{if {$que_seq_no}.{$sub_ques_no+1} % 10 == 1}
Could you please help me to correct it? Thanks in Advance.

Answer to question title
The modulo operator is % and has also alternation mod.
For more on recognized qualifiers in smarty reffer to docs: http://www.smarty.net/docsv2/en/language.function.if
Answer to your problem
So as you now know, your modulo operator is correct.
From what I can guess from your syntax, you are probably trying to access value of array by array index.
For accessing array items you have 2 possibilities:
Access by array key - syntax: $arrayVariable.key
Access by array index - syntax: $arrayVariable[index] (index is an int number) So in your case: $que_seq_no[$sub_ques_no+1]
If you are trying to access object property:
it is similar to access by array key, but operator is -> so syntax is: $objectVariable->propertyName
For more on this reffer to smarty docs: http://www.smarty.net/docsv2/en/language.variables.tpl
What probably confused you
I think you got a bit confused by usage of {} (curly braces)
- they are used to wrap the whole smarty expression - so from that it implies that they can't contain any other { or }.
A pretty example taken from smarty docs is:
{if $name == 'Fred' || $name == 'Wilma'}
...
{/if}

You are using acceptable syntax for the mod operation. I am pretty sure you're using incorrect syntax with the variable {$que_seq_no}.{$sub_ques_no+1}. Have you tried $que_seq_no[$sub_ques_no+1]?

Related

Array of value from multiple conditions

I wanna return an array of value from multiple conditions.
Currently formula is set to accept one condition.
https://docs.google.com/spreadsheets/d/1pgVlBWYKWtT6AEPyRtZmAYdOXBCjYG_B7pdlvCqYq0A/edit?usp=sharing
The desired result is showed in the ad hoc worksheet
Edit : initial problem solved by player0. Thanks !
Previous post
I'm using curly brace to return an array of value with the IF formula. This works well. I wanted to use IFS function because i wanted to use more conditions.
With a similar table, only the 1st number is returned form the array.
I don't understand why.
https://docs.google.com/spreadsheets/d/1pgVlBWYKWtT6AEPyRtZmAYdOXBCjYG_B7pdlvCqYq0A/edit?usp=sharing
Thanks !
delete range E2:L and use this in E2:
=ARRAYFORMULA(TRANSPOSE(SPLIT(TRANSPOSE(QUERY(
IF((E1:L1=B2:B11)+(E1:L1=C2:C11)+(E1:L1=D2:D11);
"1;2;3;4;5;6;7"; ";");;9^9)); ";"; 1; 0)))

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

Array behaviours in Ruby

Am a little confused about array behaviors in Ruby
Given this;
string = "abcac"
remainder = 1
What will the following function do?
string[0, remainder]
let's try to check documentation for class String. Just google rubydoc string
and https://ruby-doc.org/core-2.4.0/String.html
then look for square brackets:
str[start, length] → new_str or nil
have a nice day and good luck with coding.
Max
Thank you.
I kept researching and found that
The first operand specifies an index(which may be negative), and the second specifies the length(which must be non negative)
[index, length]

Using ampersand with an enumerator in a comparison

I have the following snippet that throws an error:
w = %w[ant dog]
w.all?(&:length > 4)
It throws an error comparison of Symbol with 4 failed (ArgumentError).
I'm not sure why &:length is a symbol and not a number. I tried:
w.all?((&:length) > 4)
but that gives me a syntax error. Is there a way to get this to work or do I have to do: w.all? { |word| word.length > 4 }.
You receive the following error, because the interpreter actually executes the following piece of code:
&:length > 4
and can't compare symbol :length with number 4.
Is there a way to get this to work or do I have to do: w.all? { |word| word.length > 4 }
You could do something like:
w.map(&:length).all? { |n| n > 4 }
But the following code is the simpliest way to go:
w.all? { |word| word.length > 4 }
Is there a way to get this to work this way w.all?(&:length > 4)?
There is no way to make it work this way because it will require you to rewrite Ruby's interpreter.
or do I have to do: w.all? { |word| word.length > 4 }
Yes, this is simple to read yet efficient way to do what you need.
I had to hunt around a bit for a good technical answer to what's going on. I found a thorough explanation here. The last section of that page explains what is happening in this particular case (but you should read the top part of the page as well).
Coming from Perl, I imagined the &:length was acting kind of like the $_ variable. This is not correct.
What is happening is that instead of passing a block to the all? method, you are passing arguments to the all? method (note the parenthesis instead of curly braces). Here, we are passing the symbol for the method length as an argument to to all?. You put the & in front of the symbol to get it to call the to_proc method (whatever that is) on the length method so it can call the length function inside the all? method for each element.

Filter an collection of tuples

I'm playing with iterables and comprehension in Julia and tried to code simple problem: find all pairs of numbers less then 10 whose product is less then 10. This was my first try:
solution = filter((a,b)->a*b<10, product(1:10, 1:10))
collect(solution)
but I got error "wrong number of arguments". This is kind of expected because anonymous function inside filter expects two arguments but it gets one tuple.
I know I can do
solution = filter(p->p[1]*p[2]<10, product(1:10, 1:10))
but it doesn't look nice as the one above. Is there a way I can tell that (a,b) is argument of type tuple and use something similar to syntax in first example?
I don't think there's a way to do exactly as you'd like, but here are some alternatives you could consider for the anonymous function:
x->let (a,b)=x; a*b<10 end
x->((a,b)=x; a*b<10)
These can of course be made into macros if you like:
macro tup(ex)
#assert ex.head == :(->)
#assert ex.args[1].head == :tuple
arg = gensym()
quote
$arg -> ( $(ex.args[1]) = $arg; $(ex.args[2]) )
end
end
Then #tup (a, b) -> a * b < 10 will do as you like.
Metaprogramming in Julia is pretty useful and common for situations where you are doing something over and over and would like specialized syntax for it. But I would avoid this kind of metaprogramming if this were a one-off thing, because adding new syntax means learning new syntax and makes code harder to read.

Resources