# String Indexing - Alright, what am I missing?

**URL:** <https://discourse.julialang.org/t/string-indexing-alright-what-am-i-missing/47018>\
**Category:** New to Julia\
**Created:** [September 21, 2020, 3:41pm UTC](https://discourse.julialang.org/t/string-indexing-alright-what-am-i-missing/47018 "2020-09-21T15:41:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![SergeantMike67](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergeantmike67/32/25103_2.png) [@SergeantMike67](https://discourse.julialang.org/u/SergeantMike67)\
**Post date:** [September 21, 2020, 3:41pm UTC](https://discourse.julialang.org/t/string-indexing-alright-what-am-i-missing/47018/1 "2020-09-21T15:41:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![affans](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/affans/32/11911_2.png) [@affans](https://discourse.julialang.org/u/affans)\
**Post date:** [September 21, 2020, 3:52pm UTC](https://discourse.julialang.org/t/string-indexing-alright-what-am-i-missing/47018/2 "2020-09-21T15:52:14Z")

</div>

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
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`.

---

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [September 21, 2020, 4:00pm UTC](https://discourse.julialang.org/t/string-indexing-alright-what-am-i-missing/47018/3 "2020-09-21T16:00:27Z")

</div>

> [@affans](#):
>
> First of all, use backticks ````` to annotate your code for readibility. Use single ticks for inline code and triple ticks for block code.

More info [here](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757) 🙂

> [@SergeantMike67](#):
>
> ```julia
> 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
> 
> ```

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

---

<div class="post-metadata">

**Author:** ![SergeantMike67](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergeantmike67/32/25103_2.png) [@SergeantMike67](https://discourse.julialang.org/u/SergeantMike67)\
**Post date:** [September 21, 2020, 4:13pm UTC](https://discourse.julialang.org/t/string-indexing-alright-what-am-i-missing/47018/4 "2020-09-21T16:13:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [September 21, 2020, 4:47pm UTC](https://discourse.julialang.org/t/string-indexing-alright-what-am-i-missing/47018/5 "2020-09-21T16:47:48Z")

</div>

> [@SergeantMike67](#):
>
> Not trying to invent anything useful per se just trying to understand how this language works.

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](https://wellesley-bisc195.github.io/BISC195.jl/stable/). It’s targeted at complete programming novices - not sure if that describes you, but it might be useful.

Did @affans answer your question though?
