Assigning data to file scope array. **Very Newbie**

Hi Everyone,

I’ll admit straight from the start, I am completely new to Odin and I find many of the terms used quite confusing. I’ve programmed a lot in the past, my first job was writing assembly language compilers so I have a reasonable programming knowledge. Currently I’m just playing, writing short bits of code to figure out the semantics.

Currently I need a file scope array which is going to hold information from a library but I’m at a loss as to how to get the info into the array and have it stay there. My really bad attempt is below, I accept I know nothing at this point, would love a little help.

I don’t get any errors but when I try to print the array the second time nothing is in it.


import "core:fmt"
import "core:strings"

myArray: [] string

main :: proc() {

	split_example()
	print_array()
	
}

split_example :: proc() {
	s := "aaa.bbb.ccc.ddd.eee"    // 5 parts
	myArray := strings.split(s, ".")
	fmt.println(len(myArray))
	fmt.println(myArray)
}


print_array :: proc () {
	for i: int = 0; i < len(myArray); i += 1 {
		fmt.println( myArray[i] )
	}	
	fmt.println(len(myArray))
}

Kindly

Ryn

You could use the private attribute to prevent access from outside of the file.

@(private="file")
myArray: []string

You are not getting the same result when you call split_example and print_array because you have created a myArray in the local scope of thesplit_exampleprocedure by using the := operator. To assign to the global variable myArray use the = operator instead.

split_example :: proc() {
	s := "aaa.bbb.ccc.ddd.eee"    // 5 parts
	myArray = strings.split(s, ".") // directly assign value to global myArray
	fmt.println(len(myArray))
	fmt.println(myArray)
}
1 Like

Ahh!!!

Thanks so much,

Easy when you know how…

Many thanks.

Ryn

Forgive my very newbish questions hopefully I’ll get up to speed quickly.

After a life time of writing assembly and boot strap code, terms like ‘rune’, ‘slice’ and ‘swizzle’ have me a bit lost. Anyway I’ll get there. Still have no idea what ‘swizzle’ means, I suspect ‘rune’ to be a visible character in a string, but definitely don’t quote me on that.

Anyway how would one go about trimming all the non-printable characters, including spaces from both the start and end of a given string. I’ve been looking through the strings package but so far have not be able to get any of the ‘Trim’ proc’s to work.

Kindly

Ryn

Okay,

Not sure this is the best way but it seems to work…

	s = strings.trim_left_space(s)
	s = strings.trim_right_space(s)

Ryn

If you’re more comfortable with assembly, a tool like godbolt might help you see what your code is doing :slight_smile:

Thanks I’ll check it out. I really do want to learn something completely new and I like everything I’ve seen and read about Odin.

It might take me a while to get fully up to speed and I have a definite goal in mind but I feel more comfortable with Odin already than other languages I’ve tried.

Ryn

1 Like

Karl Zylinski has a nice free introduction to Odin here: Introduction to the Odin Programming Language | Karl Zylinski. It elaborates a bit more than the official overview does, and explains slices and swizzling. He also explains runes and utf-8 here. (You’re right about a rune being a decoded utf-8 character - in Odin, a rune is an i32 under the hood.) If you like these explanations, I highly recommend buying his book on Odin as well.

3 Likes

Many thanks I’ll check them both out.

Odin is the first modern language I’ve tried and been able work out how to approach things without having to read reams and reams of Doco, to me that a sign of something well thought out.

I certainly don’t have the achievement badge yet, but quest accepted. :slight_smile:

Ryn

1 Like