String Indexing - Alright, what am I missing?

Quite new to Julia and am coming from a VBA background mostly Given that I am curious as to why in this code ‘h’ starts on the second letter of the string while ‘v’ starts with the first

s1 = “ACGTACT”
s2 = “ACTACGT”

for h in eachindex(s1), v in eachindex(s2)
if s1[h] == s2[v]
intCellFactor = 1
elseif s1[h] != s2[v]
intCellFactor = 0
end
end

First of all, use backticks ``` to annotate your code for readibility. Use single ticks for inline code and triple ticks for block code.

Now, I am not sure what you mean here. Are you sure you want to do the double for loop, i.e. for h in eachindex(s1), v in eachindex(s2)? The results may not be what you are expecting. For example, here is your script with a simple println statement.

julia> for h in eachindex(s1), v in eachindex(s2)
           println("index: $(h), value (s1): $(s1[h]), value (s2): $(s2[v])")
       end
index: 1, value (s1): A, value (s2): A
index: 1, value (s1): A, value (s2): C
index: 1, value (s1): A, value (s2): T
index: 1, value (s1): A, value (s2): A
index: 1, value (s1): A, value (s2): C
index: 1, value (s1): A, value (s2): G
index: 1, value (s1): A, value (s2): T
index: 2, value (s1): C, value (s2): A
index: 2, value (s1): C, value (s2): C
index: 2, value (s1): C, value (s2): T
index: 2, value (s1): C, value (s2): A
index: 2, value (s1): C, value (s2): C
index: 2, value (s1): C, value (s2): G
index: 2, value (s1): C, value (s2): T
index: 3, value (s1): G, value (s2): A
index: 3, value (s1): G, value (s2): C
index: 3, value (s1): G, value (s2): T
index: 3, value (s1): G, value (s2): A
index: 3, value (s1): G, value (s2): C
index: 3, value (s1): G, value (s2): G
index: 3, value (s1): G, value (s2): T
index: 4, value (s1): T, value (s2): A
index: 4, value (s1): T, value (s2): C
index: 4, value (s1): T, value (s2): T
index: 4, value (s1): T, value (s2): A
index: 4, value (s1): T, value (s2): C
index: 4, value (s1): T, value (s2): G
index: 4, value (s1): T, value (s2): T
index: 5, value (s1): A, value (s2): A
index: 5, value (s1): A, value (s2): C
index: 5, value (s1): A, value (s2): T
index: 5, value (s1): A, value (s2): A
index: 5, value (s1): A, value (s2): C
index: 5, value (s1): A, value (s2): G
index: 5, value (s1): A, value (s2): T
index: 6, value (s1): C, value (s2): A
index: 6, value (s1): C, value (s2): C
index: 6, value (s1): C, value (s2): T
index: 6, value (s1): C, value (s2): A
index: 6, value (s1): C, value (s2): C
index: 6, value (s1): C, value (s2): G
index: 6, value (s1): C, value (s2): T
index: 7, value (s1): T, value (s2): A
index: 7, value (s1): T, value (s2): C
index: 7, value (s1): T, value (s2): T
index: 7, value (s1): T, value (s2): A
index: 7, value (s1): T, value (s2): C
index: 7, value (s1): T, value (s2): G
index: 7, value (s1): T, value (s2): T

You can see how the double for loop works. For every index h of s1, it loops over the entire string s2.

5 Likes

More info here :slight_smile:

Let me guess… working on Needleman Wunch? This looks suspiciously like code I wrote very early on when I was learning julia :laughing:

2 Likes

kevbonham:
That is precisely what I am doing on both counts. Not trying to invent anything useful per se just trying to understand how this language works.

2 Likes

Yep - this is the best way to learn in my opinion. If you’re coming from a biology background, might as well work on biology problems. I have ~80% of a course targeted at biologists completed, piloted it this summer - feel free to take a look. It’s targeted at complete programming novices - not sure if that describes you, but it might be useful.

Did @affans answer your question though?

3 Likes