How do i: Iterate over multiple arrays in order to make strings.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By reapersremorse

im trying to turn several comma seperated strings into arrays, then use those arrays in order to make many strings.
string output order does not matter.
EXAMPLE:
var Sep = “_”
var Prefix=“apple,banana”
var Name=“good,bad”
var suffix=“mine,yours”
OUTPUT:
apple_good_mine
apple_good_yours
apple_bad_mine
apple_bad_yours
banana_good_mine
banana_good_yours
banana_bad_mine
banana_bad_yours

After a lot of trying. my current code is below.
also sorry if i didnt add the sample correctly.

`onready var PREFIX = $V/H2/PREFIX_Edit.text
onready var NAME = $V/H3/NAME_Edit.text
onready var SUFFIX = $V/H4/SUFFIX_Edit.text

onready var PreJson=to_json(PREFIX.split(“,”))
onready var NameJson=to_json(NAME.split(“,”))
onready var SuffixJson=to_json(SUFFIX.split(“,”))

#onready var PreArray=parse_json(PreJson)
#onready var NameArray=parse_json(NameJson)
#onready var SuffixArray=parse_json(SuffixJson)

onready var AllNames:Array =

func ready() → void:
var Count = 0
var PreArray:Array=[PreJson]
var NameArray:Array=[NameJson]
var SuffixArray:Array=[SuffixJson]
for A in PreArray:
for B in NameArray:
for C in SuffixArray:
var NewName = str(PreArray[Count])+"
“+str(NameArray[Count])+”_"+str(SuffixArray[Count])
#AllNames.append(NewName)
print(NewName)
Count += 1`
enter code here

the 3 lines of big text were commented out. idk why they are so big now. i only just noticed that the paste messed up all the Tabbed code. if needed i can add a picture.

reapersremorse | 2022-01-20 09:59

:bust_in_silhouette: Reply From: DaddyMonster

This ok for you?

var prefix = ["apple", "banana"]
var adjective = ["good", "bad"]
var suffix = ["mine", "yours"]

func make_string():
    for p in prefix:
        for a in adjective:
            for s in suffix:
                print(p+a+s)

OUTPUT:

applegoodmine
applegoodyours
applebadmine
applebadyours
bananagoodmine
bananagoodyours
bananabadmine
bananabadyours

my problem still persists. ive made a video showing the issue im still having.
what my code looks like

please watch if you can. my code is still broken.

reapersremorse | 2022-01-20 15:09

Oh, so cool you made a YouTube video! Thanks for that, cleared it up nicely (I hope!). Sorry for misunderstanding, the formatting made it hard to follow.

Not the prettiest / most efficient code I’ve ever written… I didn’t want to spend more than a few mins on it, bit busy. Maybe someone kind can tidy up the csv_to_array method. Anyway, it works.

func csv_to_array(csv):
	var array = []
	var item = ""
	for i in csv:
		if i == " ":
			continue
		if i == ",":
			array.append(item)
			item = ""
		else:
			item += str(i)
	array.append(item)
	return array

func _ready():
    var prefix_csv = "apple, banana"
    var adjective_csv = "good, bad"
    var suffix_csv = "mine, yours"

	var prefix = csv_to_array(prefix_csv)
	var adjective = csv_to_array(adjective_csv)
	var suffix = csv_to_array(suffix_csv)
	
	for p in prefix:
		for a in adjective:
			for s in suffix:
				print(p+a+s)

Output as before.

Btw, the trick to formatting is highlighting the section of code you want to format and then clicking on the curly braces.

DaddyMonster | 2022-01-20 15:39

Thanks so much, it never occured to me to seperate the split function from the print function. and the csv code is still good. no need to pretty it up. i do have to change a couple things but it will work perfectly.

just tried it and it does work. thanks a million DM

reapersremorse | 2022-01-20 15:56

Ah, delighted it did the job for you. Parsing is a bit of dark art, I should have used RegEx really but let’s call it a win.

DaddyMonster | 2022-01-20 16:05

this project is just a learning experience to me. im learning to code and learning gd script with this project. i have a gold fishes memory (Accident when i was a young child) so its taking me forever to do basic things.

i said all that because idk what regex is. now i need to work on configs and saving many .json files.

i really enjoy making GUIS with godot. which is why im learning code with godot lol.

reapersremorse | 2022-01-20 16:42

Ah, sounds good. I’m sure you’ll be flying in no time. Regex is a tool for messing with strings - it’s efficient and concise but you have to write gobbledygook like this \\w-(\\d+) - needless to say it’s famous for giving even experienced coders a giant headache! Ignorance is definitely bliss when it comes to RegEx. :slight_smile:

DaddyMonster | 2022-01-20 17:09