2017-12-18 5 views
1

저는 루아를 매우 신습니다. 정말 바보 같으면 미안 해요. 나는 조금이 같은 것을 수행하는 프로그램 만들려고 노력 해요 : 내가 뭘하는지 아무 생각이 없기 때문에 세계루아에서 문자열 나누기

:

사용자 입력 : "안녕하세요 세계" VAR1 : 안녕하세요 변수 2를 대입 할 때 변수를 , 내가 가진 모든 것은 test = io.read()입니다. 그리고 나는 다음에 무엇을 해야할지 전혀 모릅니다.

감사합니다.

감사합니다. Morgan. 당신이 분할 단어를 원하는 경우

+0

dublicate, 예를 들면 : [유래] (https://stackoverflow.com/questions/1426954/ 루틴 분할 문자열) –

답변

2

, 당신은 그렇게 할 수 있습니다 :

input = "Hello world" 

-- declare a table to store the results 
-- use tables instead of single variables, if you don't know how many results you'll have 
t_result = {} 

-- scan the input 
for k in input:gmatch('(%w+)') do table.insert(t_result, k) end 
-- input:gmatch('(%w+)') 
-- with generic match function will the input scanned for matches by the given pattern 
-- it's the same like: string.gmatch(input, '(%w+)') 
-- meaning of the search pattern: 
---- "%w" = word character 
---- "+" = one or more times 
---- "()" = capture the match and return it to the searching variable "k" 

-- table.insert(t_result, k) 
-- each captured occurence of search result will stored in the result table 

-- output 
for i=1, #t_result do print(t_result[i]) end 
-- #t_result: with "#" you get the length of the table (it's not usable for each kind of tables) 
-- other way: 
-- for k in pairs(t_result) do print(t_result[k]) end 

출력 :

Hello 
world 
+0

답변 주셔서 대단히 감사합니다! 그러나 나는 이것이 실제로 무엇을 의미하는지 이해하지 못합니다. 너무 신경 쓰지 않는다면, 코드의 각 부분이 무엇을하고 있는지, 어떻게 출력을 변수로 바꿀 수 있을까요? 귀하의 답변에 정말 감사드립니다. –

+0

의견을 추가했습니다. 결과의 수를 알 수없는 경우 결과를 단일 변수에 저장하는 것은 의미가 없습니다. 그리고 사용자가 입력을하면 몇 단어가 될지 모릅니다. – McBarby