This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
AndreWalia's profile picture

word value NEED HELP

Started by AndreWalia, 27 February 2013 - 02:56 PM
AndreWalia #1
Posted 27 February 2013 - 03:56 PM
first off, a dollar word is when the value of a word is 100
the value of a word is (a = 1, b = 2, c = 3, d = 4….z = 26) so db == 6 (4+2)
I am getting an error at line 15 which i dont know why
this is supposed to print the value of the word.
pastebin get sz3v5Fv0
I have asked multiple people and I can not figure this out. Thanks
SuicidalSTDz #2
Posted 27 February 2013 - 04:34 PM
You will most likely need to store all the letters in a table with their values and go from there. I'm tired so i'm not going to make any "code" <wink wink> because it will most likely be wrong ;)/> But i'm sure the other CC members will be happy to help you.

EDIT: Hmm, really tired. Didn't even interperet his question correctly, nor did I see the pastebin link… Well with that derp, imma opt out of this thread ;)/>
GopherAtl #3
Posted 27 February 2013 - 04:41 PM
table.insert affects the table you pass directly, and returns nothing, so instead of "x=table.insert(x,input:sub(y,y))" it should just be "table.insert(x,input:sub(y,y))"

that should get it working. Some other random tips to possibly improve the program:

instead of looping through an array of all letters to find the index of the right one, turn it around, make the table use the letters themselves as keys and have the numbers as values, ex:


abc={
  a=1, b=2, c=3, ...etc
}
That way, instead of looping through the table, you can look up the value directly, ex, "abc["c"]" would give the value stored for c, 3.

Also, a forum tip, you said the error was on line 15, but in the code as you actually put it on pastebin, the error was on line 13. This was either a mistake on your part, or you removed 2 lines somewhere in the program. You'll get quicker and better responses in the future if you double-check that the line number you report matches the code as you actually shared it. The full error message, rather than just the line number, would also be helpful.
nobody1717 #4
Posted 27 February 2013 - 08:41 PM
Local Chars = {
a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z
}
Function find(letter)
For i,j in pairs(chars) do
If j == letter then
return i
End
End
End
Local Word = read()
TotalCount = 0
For i = 1, str.len(word) do
Letter = Str.sub(word,i,i)
TotalCount = TotalCount + find(letter)
End
Print("that word is worth : "..TotalCount)
nobody1717 #5
Posted 27 February 2013 - 08:43 PM
And at 13, dont do
X = table.insert
Just table.insert.
GopherAtl #6
Posted 27 February 2013 - 09:08 PM
Local Chars = {
a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z
}
Function find(letter)
For i,j in pairs(chars) do
If j == letter then
return i
End
End
End
Local Word = read()
TotalCount = 0
For i = 1, str.len(word) do
Letter = Str.sub(word,i,i)
TotalCount = TotalCount + find(letter)
End
Print("that word is worth : "..TotalCount)

that is, conservatively, 50x more broken than the code in the OP, which had only the one minor error you noted in your follow-up post 2 minutes later.
LordIkol #7
Posted 28 February 2013 - 12:37 AM
Hi SuperSmartKid,

Try this Code it should do what you want. Used the Method Gopher suggested already cause i think this is the easiest way.


local function Countit()
abc={A=1, B=2, C=3, D=4, E=5, F=6, G=7, H=8, I=9, J=10 ,K=11, L=12, M=13, N=14, O=15, P=16, Q=17, R=18, S=19, T=20, U=21, V=22, W=23, X=24, Y=25, Z=26,a=1, b=2, c=3, d=4, e=5, f=6, g=7, h=8, i=9, j=10, k=11, l=12, m=13, n=14, o=15, p=16, q=17, r=18, s=19, t=20, u=21, v=22, w=23, x=24, y=25, z=26
}
local TotalCount =0
print("test")
local word=read()
for i=1, #word do
local Letter = string.sub(word,i,i)
TotalCount = TotalCount + abc[Letter]
end
print(TotalCount)
end
Countit()

Greets Loki
remiX #8
Posted 28 February 2013 - 01:35 AM
Use a table ( didn't put whole alphabet - takes too long :P/>/> )

local chars = {
	["a"] = 1,
	["b"] = 2,
	["c"] = 3,
	["d"] = 4,
	["e"] = 5,
	["f"] = 6,
	["g"] = 7,
	["h"] = 8,
	["i"] = 9,
	["j"] = 10,
	["k"] = 11,
}

local function count( str )
	local c = 0
	for i = 1, #str do
		c = c + chars[ str:sub( i, i ):lower() ]
	end
	return c
end

local textToCount = "CAB"
local textCount = count( textToCount )

print(textCount)
theoriginalbit #9
Posted 28 February 2013 - 01:55 AM
Alternatively, over using tables and having lots of typing you can realise that in ASCII 'A' is 65 up to 'Z' which is 90. So with this information we can convert our string to uppercase and process it. If we get our uppercase string and loop over it we can then use string.byte, which gives us the ASCII code for that character. we can then minus 64 from this code to get our scaled letter from 1-26… and just add it together. WAY easier than typing out a table and such, imo.

The code:

local text = "abcd"

local function count(str)
  str = str:upper()
  local count = 0
  for i = 1, #str do
	local char = str:sub(i,i)
	count = count + char:byte() - 64 -- Capital A is ASCII 65
  end
  return count
end

print(text)
print(count(text))

Alternatively if you wanted to use a table, like remiXs solution, you could avoid typing it out by using a loop like so:

for i = 65, 90 do
  tTable[ string.char( i ) ] = ( i - 64 )
end
nobody1717 #10
Posted 01 March 2013 - 05:31 PM
Local Chars = {
a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z
}
Function find(letter)
For i,j in pairs(chars) do
If j == letter then
return i
End
End
End
Local Word = read()
TotalCount = 0
For i = 1, str.len(word) do
Letter = Str.sub(word,i,i)
TotalCount = TotalCount + find(letter)
End
Print("that word is worth : "..TotalCount)

that is, conservatively, 50x more broken than the code in the OP, which had only the one minor error you noted in your follow-up post 2 minutes later.

Besides the capital letters (im too lazy to edit them out)
What is wrong with it?
remiX #11
Posted 01 March 2013 - 05:53 PM
–snip

that is, conservatively, 50x more broken than the code in the OP, which had only the one minor error you noted in your follow-up post 2 minutes later.

Besides the capital letters (im too lazy to edit them out)
What is wrong with it?

Looks like you wrote it on your phone :P/>

a problem besides the capital letters is the table, each letter needs to be encased with inverted commas
theoriginalbit #12
Posted 01 March 2013 - 05:55 PM
What is wrong with it?
  • Its not in code tags.
  • its 'end' not 'End'
  • its 'str' not 'Str'
  • its 'print' not 'Print'
  • its 'local' not 'Local'
  • basically you are using different cases all over the place.
  • the elements in a table need to be in " " or ' ' EDIT: damn ninja'd on this one
  • there is no need to use pairs for a simple table, use a incremental for loop instead, using a generic for loop confuses people
If you're going to give advice, at least make sure that it will run, not everyone is able to pick up on these simple mistakes and it can cause bigger mistakes that someone else will need to come and help with.
Kingdaro #13
Posted 02 March 2013 - 02:07 AM
I'd use a string of the alphabet and then use :find(), then add the letter's position to the total.


local letters = 'abcdefghijklmnopqrstuvwxyz'

while true do
  local input = read()
  local total = 0
  for v in input:gmatch '.' do
    total = total + (letters:find(v:lower()) or 0)
  end
  print(total)
end