how to make a function that prints a variable number of parameters in pascal? - variables

I want to let the user give me a variable number of strings (as virables).
example :
begin
cout('Hello').(' ').('world')
end.
this will print: "Hello world"
I know I just can let him input a string but I want to this code to work...
I think a record will help nut I dont know how
thank you

Im not sure what is that code example you have written ... but i try to help you.
Program test;
uses crt;
var string1,string2,string3:string;
begin
write("Write to first string : "); readln(string1);
write("Write to second string: "); readln(string2);
write("Write to third string : "); readln(string3);
String2:= string1 +" "+ string3; // it will add first with third string to one with one space
String3:= string2 +" "+ string1;// also 2.+ 1.
clrscr; //clearscreen (in CRT)
writeln("1. = 1.");
writeln("2. = 1. + 3.");
writeln("3. = 2. + 1.");
writeln;
writeln(string1);
writelm(string2);
writeln(string3);
I havent tested this (i have written it now for you) i think you will read it and learn how to addict or how to do simple with strings.

Related

Getting the name of the variable as a string in GD Script

I have been looking for a solution everywhere on the internet but nowhere I can see a single script which lets me read the name of a variable as a string in Godot 3.1
What I want to do:
Save path names as variables.
Compare the name of the path variable as a string to the value of another string and print the path value.
Eg -
var Apple = "mypath/folder/apple.png"
var myArray = ["Apple", "Pear"]
Function that compares the Variable name as String to the String -
if (myArray[myposition] == **the required function that outputs variable name as String**(Apple) :
print (Apple) #this prints out the path.
Thanks in advance!
I think your approach here might be a little oversimplified for what you're trying to accomplish. It basically seems to work out to if (array[apple]) == apple then apple, which doesn't really solve a programmatic problem. More complexity seems required.
First, you might have a function to return all of your icon names, something like this.
func get_avatar_names():
var avatar_names = []
var folder_path = "res://my/path"
var avatar_dir = Directory.new()
avatar_dir.open(folder_path)
avatar_dir.list_dir_begin(true, true)
while true:
var avatar_file = avatar_dir.get_next()
if avatar_file == "":
break
else:
var avatar_name = avatar_file.trim_suffix(".png")
avatar_names.append(avatar_name)
return avatar_names
Then something like this back in the main function, where you have your list of names you care about at the moment, and for each name, check the list of avatar names, and if you have a match, reconstruct the path and do other work:
var some_names = ["Jim","Apple","Sally"]
var avatar_names = get_avatar_names()
for name in some_names:
if avatar_names.has(name):
var img_path = "res://my/path/" + name + ".png"
# load images, additional work, etc...
That's the approach I would take here, hope this makes sense and helps.
I think the current answer is best for the approach you desire, but the performance is pretty bad with string comparisons.
I would suggest adding an enumeration for efficient comparisons. unfortunately Godot does enums differently then this, it seems like your position is an int so we can define a dictionary like this to search for the index and print it out with the int value.
var fruits = {0:"Apple",1:"Pear"}
func myfunc():
var myposition = 0
if fruits.has(myposition):
print(fruits[myposition])
output: Apple
If your position was string based then an enum could be used with slightly less typing and different considerations.
reference: https://docs.godotengine.org/en/latest/tutorials/scripting/gdscript/gdscript_basics.html#enums
Can't you just use the str() function to convert any data type to stirng?
var = str(var)

How do I read the data from a TYPE_MIME_PART item?

It kinda works, but the problem is that it seems that the MIME_PART structure is not initialized ? all it's properties has the same values, even if I try to open a different mime item.
MIME_PART *pMime;
DHANDLE hPart;
char *pText;
WORD textLen;
if (error = NSFMimePartGetPart(bidLinksItem, &hPart)) {
goto exit;
}
pMime = OSLock(MIME_PART, hPart);
textLen = (pMime->wByteCount) - pMime->wHeadersLen - pMime->wBoundaryLen;
pText = (char *)pMime + sizeof(MIME_PART) + wHeadersLen;
char *itemText = (char *)malloc(textLen);
memcpy(itemText, pText, textLen);
itemText[textLen] = '\0';
OSUnlock(hPart);
The itemText string has most of the content, but since the MIME_PART structure is not properly set, the pointer to the text is off...
So how do I properly set the MIME_PART?
Your code should do something like this instead:
DHANDLE hPart;
char *pchPart;
if (error = NSFMimePartGetPart(bidLinksItem, &hPart)) {
goto exit;
}
pchPart = OSLock(char, hPart);
In other words, lock the handle as type char instead of type MIME_PART. At this point, pchPart points to the beginning of the raw part data -- starting with a boundary (if present) and the headers. You can use NSFMimePartGetInfoByBLOCKID to get the length of the boundary and headers.
I realize this contradicts the documentation, but I've confirmed with a subject matter expert: The documentation is wrong.
Wrong answer, but the comments may be useful. My other answer is more correct.
This question could be improved. For example, you could show some sample data and describe the results when you try to read that data with your code.
But I'll try to answer based on the information I have. You calculated the text length like this:
textLen = (pMime->wByteCount) - pMime->wHeadersLen - pMime->wBoundaryLen;
That looks right to me, but then you do this:
pText = (char *)pMime + sizeof(MIME_PART) + wHeadersLen;
Is wHeadersLen guaranteed to be equal to pMime->wHeadersLen? Also, you didn't consider the boundary length. Shouldn't you calculate the address like this instead?
pText = (char *)pMime + sizeof(MIME_PART) + pMime->wHeadersLen + pMime->wBoundaryLen;

Select everything from //

I'm making like a "mini programming language" in visual basic.
Mostly just for practice and for fun.
I just have one problem. I want to make a commenting system.
I got an idea how it would work, but i don't know how to do it.
So this is what i want to do:
I want to start to select all text from //
So for example, if i write:
print = "Hello World!"; //This is a comment!
it will select everything from the // so it will select
//This is a comment!
Then i would just replace the selected text with nothing.
You can use String.IndexOf + Substring:
Dim code = "Dim print = ""Hello World!""; //This is a comment!"
Dim indexOfComment = code.IndexOf("//")
Dim comment As String = Nothing
If indexOfComment >= 0 Then comment = code.Substring(indexOfComment)
If you want the part before the comment dont use String.Replace but also Substring or Remove:
code.Substring(0, indexOfComment)

How do you read a variable from another variable

Horse_Apple = "Happy Horse"
local var = Animal() .. "_" .. Food()
print(var)
I hope someone here understands the problem i'm trying to solve here. Animal() returns "Horse" and Food() returns "Apple".
What i'm attempting to do is read the variable 'var' and read its value 'Horse_Apple' as a variable which should return "Happy Horse". As much as im trying to find the solution to this im failing big time, Thank you.
You can access a global variable by a dynamic name using _G, i.e.:
print(_G[var])
Normally this isn't considered good design: It's better to make Horse_Apple a key in some table and access that table instead, like this:
values = { Horse_Apple="Happy Horse" }
local var = Animal() .. "_" .. Food()
print values[var]

Using perl regex constructs in VB.Net?

In perl you can write
$string =~ tr/[a,e,i,o,u,y]/[A,E,I,O,U,Y]/;
for example.
Is it possible to achieve the same "translation" effects with VB.Net regexes?
Thanks you!
PS: I'm not searching for a way to port this very example, it's more of a curiosity question :)
There is no standard method for this. You can do it by iterating over each character in your input string and using a dictionary to map it to another character (or leave it unchanged if the character is not found in the dictionary). The result can be built using a StringBuilder for performance reasons.
If performance is not an issue then you might be able to use a few replace operations instead:
s = s.Replace("a", "A")
.Replace("e", "E")
...
.Replace("y", "Y");
Here's one way to do this:
public string fakeTR(string theString, char[] org, char[] rep)
{
for(int i=0;i<org.lenght;i++)
{
theString = theString.Replace(org[i], rep[i]);
}
return theString;
}
You would be able to call it with somewhat clunky but shorter:
string v = "Black in South Dakota";
v = fakeTR(v, new char[]{'B','l','a','c','k'}, new char[]{'W','h','i','t','e'});
H/T http://discuss.joelonsoftware.com/default.asp?dotnet.12.306220.6