julia on raspian buster – string manipulations

In between today’s yard and house chores, I’ve continued to pull down example code from Julia By Example. This post’s code example has begun to diverge even more than the earlier examples I downloaded and reported about in the last post. There’s so much that’s changed here I don’t even know where to start.

I draw you attention to the highlighted lines below, 39-43. Lines 39-40 are commented original Julia command and output. There is no matchall() anymore; I tested with version 1.0.4 as well as 1.2.0 RC3. I tried to recreate the functionality with collect(eachmatch(…)). And as you’ll note the new output structure doesn’t even come close to the output from matchall(). I guess that’s something I can figure out as I dive deeper into Julia.

One other thing I’ve discovered, and that’s all the links in the code as comments are broken. None of them go to where they’re supposed to. The websites are still there but the specific pages that correspond to specific functions in the example do not. I’ve checked all of them so far and they’re all broken.

I don’t know why they leave that site up. It’s misleading unless you take it as a personal challenge to fix it all and bring it all up to date. I’m doing that right now because it’s helping me to find the real up-to-date sources, and it’s beginning to stick. My latest sources for correct Julia (so far) are Introducing Julia (https://en.wikibooks.org/wiki/Introducing_Julia) and Julia 1.1 Documentation (https://pkg.julialang.org/docs/julia/THl1k/1.1.1/index.html). I’m sure there are others, and I’ll eventually find them. but for now these seem to be sufficient.

#!/usr/bin/env julia
s1 = "The quick brown fox jumps over the lazy dog α,β,γ"

# find the first instance in the string of a given character.
i = findfirst(isequal('b'), s1)
#> 11

# or a range if called with another string
r = findfirst("brown", s1)
#> 11:15

# replace one substring with another.
r = replace(s1, "brown" => "red")
show(r); println()
#> "The quick red fox jumps over the lazy dog"

# findfirst can also take a regular expressions by preceding the string with 'r'.
r = findfirst(r"b[\w]*n", s1)
#> 11:15

# again with a regular expression
r = replace(s1, r"b[\w]*n" => "red")
show(r); println()
#> "The quick red fox jumps over the lazy dog"

# there are also functions for regular expressions that return RegexMatch types
r = match(r"b[\w]*n", s1)
#> RegexMatch("brown")

# RegexMatch types have a property match that holds the matched string
show(r.match); println()
#> "brown"

# r = matchall(r"[\w]{4,}", s1)
#> SubString{UTF8String}["quick","brown","jumps","over","lazy"]
r = collect(eachmatch(r"[\w]{4,}", s1))
#> RegexMatch[RegexMatch("quick"), RegexMatch("brown"), RegexMatch("jumps"), RegexMatch("over"), RegexMatch("lazy")]
r = eachmatch(r"[\w]{4,}", s1)
for i in r print("\"$(i.match)\" ") end
#> "quick" "brown" "jumps" "over" "lazy" 

r = "hello "^3
show(r); println() #> "hello hello hello "

# e.g., with one argument it strips the outer whitespace
r = strip("hello ")
show(r); println() #> "hello"
# or with a second argument of an array of chars it strips any of them;
r = strip("hello ", ['h', ' '])
show(r); println() #> "ello"
# (note the array is of chars and not strings)

r = split("hello, there,bob", ',')
show(r); println() #> ["hello"," there","bob"]
r = split("hello, there,bob", ", ")
show(r); println() #> ["hello","there,bob"]
r = split("hello, there,bob", [',', ' '], limit=0, keepempty=false)
show(r); println() #> ["hello","there","bob"]

r = join(collect(1:10), ", ")
println(r) #> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10