I would like a create a new column that is created by splitting the cell called string by the “.” delimiter and removing the first and last sections of the field.
Here’s my attempt at turning "A.B.C.D.JUNK" into ["B.C.D","B.C","B"]. I split it up into multiple lines for clarity. You can collapse it back to a one-liner later.
s = "A.B.C.D.JUNK"
ps = split(s, ".")
middle = ps[2:end-1]
newstring = reduce(middle) do m, a
if ismissing(m)
return [a]
else
return ["$(m[1]).$a", m...]
end
end
The variable newstring should be ["B.C.D","B.C","B"]. The main difference between your attempt and mine is join vs reduce.
@g-gundam I’m not sure how this works to update the data frame? working on one string at a time, the join(split) works fine, but I’m having trouble updating the entire column in the dataframe.
Just because you can, doesn’t mean you should.
Sometimes, Julia reminds me of Perl, and I mean that affectionately. I liked Perl and how flexible and playful it was in its time.
It would not work, because the character just before the last . could be muliti-byte:
julia> s = ".ą."
".ą."
julia> SubString(s,findfirst('.',s)+1:findlast('.',s)-1)
ERROR: StringIndexError: invalid index [3], valid nearby indices [2]=>'ą', [4]=>'.'
(things are hard :(, I made such mistakes numerous times unfortunately, I work in the Polish language where such cases are super common so I am sensitive to them). You need to chop or take prevind