Syntax error of getindex in my code

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

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))
 


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

3 Likes

Here is an example how to read the error message:

 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 and it even tells you that you have an invalid iteration specification

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 ?

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

for c in a

instead of

for(c in a)

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

This is the second topic from you that looks like homework.

It is fine to ask about homework here, but please check our (currently informal) homework policy, 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.

2 Likes
lettercounts = zeros(Int,27)

and

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

and

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

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

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)

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

thanks a lot , i appreciate that.

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

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

julia> a="abcd"
"abcd"

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

so try

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.

1 Like

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.