Can anyone explain me in depth about variables? - android-studio

Hi I have been recently learning about variables and values in Kotlin. But after reading many articles and watching endless hours of tutorials I still can't comprehend it. I was hoping someone could enlighten me.
I understand that a value is inmutable and this implies it does not change. and a variable it does. But what I don't get is this :
fun main () {
Var myFirstVariable= "Hello"
println(myFirstVariable)
myFirstVariable = "Nice to meet you"
println(myFirstVariable)
}
Now my question is why would someone need a variable if the first variable is cancelled by the second value of the assigned variable ?
I just could use a value and save myself some time, and code.
Can somebody clarify this to me?

A bit weird but i will explain with just 1 example...
Imagine, you have a score variable. Whenever your player move, you will increase this, right? Maybe we need that in this case? Also if you want, you can do static variables, example "PlayerSpeed = 100"; like this, you can make this variable static, so i think you can use it whenever you need it.

Related

Why doesn't my excel vba if/or statement work correctly?

I'm comparing values of numbers from 2 data sheets, and I've dropped the relevant data from both into their own arrays. I need to find matching values to run other steps of analysis.
For i = Lbound(Array1) to UBound(Array1)
For j = LBound(Array2) to UBound(Array2)
If (criteria for Array2) then
variable = 11111
Else
variable = 22222
End if
If variable = Array1(i,1) Or variable = Array1(i,2) or variable = Array1(i,3) then
more steps
Else
more steps
End if
next j
next i
The first if statement sets the variable correctly, but the variable doesn't match any of the criteria. It doesn't go to the else like it should. Now I only know this because I walked through the code step by step. If I just F5 and run the thing, "Excel is not responding". I don't know what the hang up it. All of my variables are declared and assigned a type, I'm not missing any closing statements. And I have no idea what I'm doing wrong.
What do I need to check for in my code?
EDIT
Sorry, but in this instance I'm not allowed to upload any code here. It's work related, NDA kind of stuff. Hence the pseudo code. What I need to show wouldn't be a big deal(at least I don't think it would), but I'm not risking it.
My apologies.
The solution, as it turns out, has to do with a poorly named array(not me) and a simple typo(definitely me). I'm certain that would have been an easy solve for the good citizens of Stack Overflow if I would have been allowed to post actual code.
For what's it worth, I think it's dumb that I couldn't in this case. Thanks #ScottCraner and #SuperSymmertry for trying to be helpful even without much to go on.
Super, I'm still curious about Val. If you've got a minute, I would appreciate more knowledge on that. Anything from an actual person is better than Microsoft documentation.

Matlab function defintion method that takes string as the definition (MFILE)

there is this method I used the other day and I have forgotten the details, which in we used a syntax like this:
f=//command//(x,'sin(x)');
something like this.
im not sure if the syntax is fully correct, or what the right command is. but after this we could simply ask for the f(x) value like this:
x= 0;
y= f(x);
and then the results were y=0;
What you are asking for is usually not recommendable. Please check if a simple anonymous function also fits your requirements:
f=#(x)(sin(x))
In case you really need to evaluate from a string:
f=str2func('#(x)sin(x)')
I would advice against the second option unless absolutely required, it can lead to hard to debug errors.
well I found the answer myself and it was "inline" command; :)
f=inline('sin(x+y+z)','x','y','z');
you can add as much variables as needed too.

Write text to file function python

so I would like to ask you for help.
I was scooping and sweeping the internet so long for that and I did not find how to do it. What I just produced is totally wrong and my professor did bad time with formulating the task. it is entry level task on university I have already finished the hardest ones, but this one I just dont get what he wants
Translation is made by me so it might be little bit off, but bear with me, please.
"Create file "linewriter.py". This file will have function writeTextToFile, that will accept one parameter and on end is also one parameter. Function will take the parameter on input and chain it with variable STATIC_TEXT (stated bellow). Queue of chaining will be as follows: 1st static text and then argument of function. This way chained input will write to file of any given name and a the name of this file will be returned as input parameter of function.
STATIC_TEXT: “This is my static text which must be added to file. It is very long text and I do not know what they want to do with this terrible text. ”
...
What? I really don't get what he want's from me. Can you help? Thank you all :)
STATICKY_TEXT = "This is my static text which must be added to file. It is very long text and I do not know what they want to do with this terrible text.\nNew Line! "
saveFile = open ("exampleFile.txt","w")
saveFile.write(STATICKY_TEXT)
saveFile.close()
solved it :) but I COULD not figure it out for days haha ... silly me

Executing functions stored in a string

Lets say that there is a function in my Delphi app:
MsgBox
and there is a string which has MsgBox in it.
I know what most of you are going to say is that its possible, but I think it is possible because I opened the compiled exe(compiled using delphi XE2) using a Resource Editor, and that resource editor was built for Delphi. In that, I could see most of the code I wrote, as I wrote it. So since the variables names, function names etc aren't changed during compile, there should a way to execute the functions from a string, but how? Any help will be appreciated.
EDIT:
What I want to do is to create a simple interpreter/scripting engine. And this is how its supposed to work:
There are two files, scr.txt and arg.txt
scr.txt contains:
msg_show
0
arg.txt contains:
"Message"
And now let me explain what that 0 is:
First, scr.txt's first line is function name
second line tells that at which line its arguments are in the arg.txt, i.e 0 tells that "Message" is the argument for msg_show.
I hope my question is now clear.
I want to make a simple scripting engine.
In order to execute arbitrary code stored as text, you need a compiler or an interpreter. Either you need to write one yourself, or embed one that already exists. Realistically, the latter option is your best option. There are a number available but in my view it's hard to look past dwscript.
I think I've already solved my problem! The answer is in this question's first answer.
EDIT:
But with that, as for a workaround of the problem mentioned in first comment, I have a very easy solution.
You don't need to pass all the arguments/parameters to it. Just take my example:
You have two files, as mentioned in the question. Now you need to execute the files. It is as simple as that:
read the first line of scr.txt
check if it's a function. If not, skip the line
If yes, read the next line which tells the index where it's arguments are in arg.txt
pass on the index(an integer) to the "Call" function.
Now to the function which has to be executed, it should know how many arguments it needs. i.e 2
Lets say that the function is "Sum(a,b : integer)".It needs 2 arguments
Now let the function read the two arguments from arg.txt.
And its done!
I hope it will help you all.
And I can get some rep :)

Netlogo: Creating subsets of agentsets of a particular breed

I am still new to Netlogo, but I can not find an explanation for this in the documentation.
I am trying to create a subset of an agentset that only contains one type of breed. It would seem that I could use "with" to perform this, but for some reason that does not work.
This code works:
ask link-neighbors with [shape = "person"][
set pmt (pmt + dist)
]
But this code does not:
ask link-neighbors with [breed = "psngrs"][
set pmt (pmt + dist)
]
How can I create a subset of an agentset with only this particular breed?
Thanks!
This question is showing as unanswered, though Alan gave the correct answer in a comment. So, just so I stop clicking on this thinking it is unanswered, I'm going to reiterate what he said as an answer. Alan, if you add your comment as answer, I'll happily delete mine.
Anyway, just get rid of the quotes around the breed name, like so:
link-neighbors with [breed = psngrs]

Resources