# Syntax error of getindex in my code

**URL:** <https://discourse.julialang.org/t/syntax-error-of-getindex-in-my-code/29039>\
**Category:** New to Julia\
**Tags:** homework\
**Created:** [September 22, 2019, 10:32am UTC](https://discourse.julialang.org/t/syntax-error-of-getindex-in-my-code/29039 "2019-09-22T10:32:59Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fuad\_Sami](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fuad_sami/32/16900_2.png) [@Fuad\_Sami](https://discourse.julialang.org/u/Fuad_Sami)\
**Post date:** [September 22, 2019, 10:33am UTC](https://discourse.julialang.org/t/syntax-error-of-getindex-in-my-code/29039/1 "2019-09-22T10:33:00Z")

</div>

this is the error : LoadError(“main.jl”, 5, ErrorException(“syntax: invalid iteration specification”))

```julia
function makeAnagram(a::String,b::String)
 
lettercounts = zeros(Int,27)
      for i in a
      a1 = a[i]
          lettercounts[Int(a1) - Int('a')] += 1
      end
      for i in b
      b1 = b[i]
          lettercounts[Int(b1) - Int('a')] -= 1
      end
      result = 0
      for i in lettercounts
     result += abs(i)
end
      return result
 
 
end
a="abcd"
b = "abcd"
println("Total " , makeAnagram( a,b))
 

```

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [September 22, 2019, 11:04am UTC](https://discourse.julialang.org/t/syntax-error-of-getindex-in-my-code/29039/2 "2019-09-22T11:04:43Z")

</div>

I often have errors in my code…

In this case, check the docs:  
[https://docs.julialang.org/en/v1/manual/control-flow/#man-loops-1](https://docs.julialang.org/en/v1/manual/control-flow/#man-loops-1)

---

<div class="post-metadata">

**Author:** ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)\
**Post date:** [September 22, 2019, 11:09am UTC](https://discourse.julialang.org/t/syntax-error-of-getindex-in-my-code/29039/3 "2019-09-22T11:09:54Z")

</div>

Here is an example how to read the error message:

```julia
 LoadError(“main.jl”, 5, ErrorException(“syntax: invalid iteration specification”))

```

- **`LoadError`** : "An error occurred while including, requiring or using a file. (you can type `?LoadError` into the Julia REPL to get the description
- **`main.jl`** : the file where the error happened
- **`5`** : the line number
- **`“syntax: invalid iteration specification”`** : this means it’s a [syntax error](https://en.wikipedia.org/wiki/Syntax_error) and it even tells you that you have an `invalid iteration specification`

---

<div class="post-metadata">

**Author:** ![Fuad\_Sami](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fuad_sami/32/16900_2.png) [@Fuad\_Sami](https://discourse.julialang.org/u/Fuad_Sami)\
**Post date:** [September 22, 2019, 11:39am UTC](https://discourse.julialang.org/t/syntax-error-of-getindex-in-my-code/29039/4 "2019-09-22T11:39:16Z")

</div>

yes i see, but i still dont know how to fix it and i can’t find the solution in any file in julia.

i need to convert the first letter in the word to int in for loop  
example :  
for letter in word , word = dog  
the letter ‘d’ convert to number  
and o and g

any one ?

---

<div class="post-metadata">

**Author:** ![BeastyBlacksmith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/beastyblacksmith/32/4741_2.png) [@BeastyBlacksmith](https://discourse.julialang.org/u/BeastyBlacksmith)\
**Post date:** [September 22, 2019, 11:51am UTC](https://discourse.julialang.org/t/syntax-error-of-getindex-in-my-code/29039/5 "2019-09-22T11:51:17Z")

</div>

We don’t use brackets for for-loops in julia, e.g.

```julia
for c in a

```

instead of

```julia
for(c in a)

```

---

<div class="post-metadata">

**Author:** ![Fuad\_Sami](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fuad_sami/32/16900_2.png) [@Fuad\_Sami](https://discourse.julialang.org/u/Fuad_Sami)\
**Post date:** [September 22, 2019, 11:59am UTC](https://discourse.julialang.org/t/syntax-error-of-getindex-in-my-code/29039/6 "2019-09-22T11:59:17Z")

</div>

thanks , i still have some error, can take a look to the code again ?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [September 22, 2019, 12:06pm UTC](https://discourse.julialang.org/t/syntax-error-of-getindex-in-my-code/29039/7 "2019-09-22T12:06:45Z")

</div>

This is the second topic from you that [looks like](https://discourse.julialang.org/t/makeanagram-there-is-an-error-that-i-cannot-find-in-my-code/29032) homework.

It is fine to ask about homework here, but please check our (currently informal) [homework policy](https://discourse.julialang.org/t/homework-policy/2342/17), and invest some time in learning the basics of the language first, otherwise your experience with Julia may be needlessly frustrating. If your course has a TA, that person could also be a valuable resource.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [September 22, 2019, 12:14pm UTC](https://discourse.julialang.org/t/syntax-error-of-getindex-in-my-code/29039/8 "2019-09-22T12:14:05Z")

</div>

```julia
lettercounts = zeros(Int,27)

```

and

```julia
lettercounts[asc - Int('a')] += 1

```

and

```julia
for i in lettercounts
      result += abs(i)
end

```

To get the first letter in a string just use indexing:

```julia
julia> t="abcd"
"abcd"

julia> t[1]
'a': ASCII/Unicode U+0061 (category Ll: Letter, lowercase)

julia> t[2]
'b': ASCII/Unicode U+0062 (category Ll: Letter, lowercase)

```

> [@Tamas\_Papp](#):
>
> This is the second topic from you that [looks like](https://discourse.julialang.org/t/makeanagram-there-is-an-error-that-i-cannot-find-in-my-code/29032) homework.
> 
> It is fine to ask about homework here, but please check our (currently informal) [homework policy](https://discourse.julialang.org/t/homework-policy/2342/17), and invest some time in learning the basics of the language first, otherwise your experience with Julia may be needlessly frustrating. If your course has a TA, that person could also be a valuable resource.

Thats why I only help with syntax issues. I don’t comment to the algorithm.

---

<div class="post-metadata">

**Author:** ![Fuad\_Sami](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fuad_sami/32/16900_2.png) [@Fuad\_Sami](https://discourse.julialang.org/u/Fuad_Sami)\
**Post date:** [September 22, 2019, 12:30pm UTC](https://discourse.julialang.org/t/syntax-error-of-getindex-in-my-code/29039/9 "2019-09-22T12:30:59Z")

</div>

thanks a lot , i appreciate that.

---

<div class="post-metadata">

**Author:** ![Fuad\_Sami](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fuad_sami/32/16900_2.png) [@Fuad\_Sami](https://discourse.julialang.org/u/Fuad_Sami)\
**Post date:** [September 22, 2019, 2:44pm UTC](https://discourse.julialang.org/t/syntax-error-of-getindex-in-my-code/29039/10 "2019-09-22T14:44:09Z")

</div>

this code stil with error  
LoadError(“main.jl”, 22, MethodError(getindex, (“abcd”, ‘a’), 0x00000000000063e2))

i try to fix it, its work fine  
i think the error is in **a1 = a[i]** any one can help me ? this is not homework or some thing i just learn the language endividually and i try to solve some qestions alone

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [September 22, 2019, 2:57pm UTC](https://discourse.julialang.org/t/syntax-error-of-getindex-in-my-code/29039/11 "2019-09-22T14:57:25Z")

</div>

> [@Fuad\_Sami](#):
>
> for i in a

doesn’t give you an index i, it gives you already the single characters of string a:

```julia
julia> a="abcd"
"abcd"

julia> for i in a
       println(i)
       end
a
b
c
d

```

so try

```julia
for i in a
    lettercounts[Int(i) - Int('a')] += 1
end

```

By the way, don’t edit your code in the OP, just paste new code in your new answer. In this way the thread has some meaning in the answers before and others can see how it evolves.

---

<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 22, 2019, 6:14pm UTC](https://discourse.julialang.org/t/syntax-error-of-getindex-in-my-code/29039/12 "2019-09-22T18:14:50Z")

</div>

> [@oheil](#):
>
> By the way, don’t edit your code in the OP, just paste new code in your new answer.

This is good advice, but it would be nice for you to edit the title to something more descriptive of the error so that future people searching can find the answer.
