Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

hi . about string searching etc.

sorry to trouble here.

when I write " io. " in codea, there are " close , flush, input , lines open ---read---write "etc shown.

but cannot find reference in codea. where can I find the explaination of above. ?

this is found when I want to do something on txt file. is there a easy way to handle line in txt file ?

my text is normally like this :smile:

[00:00.00][101]Unit one Hello! \n[101]
[00:04.80][101]Hello! I'm Zoom. \n[101]
[00:07.82][101]Hi! My name's Zip. [101]
[00:11.11][101]Hello! I'm Mike. \n[101]
[00:15.68][101]Hi, I'm Wu Yifan.\n [101]

[00:18.52][102]Hello, I'm Chen Jie. \n[102]
[00:21.50][102]What's your name? \n[102]
[00:23.05][102]My name's Sarah.\n [102]
[00:25.82][102]Goodbye! \n[102]
[00:27.88][102]Bye, Miss White! \n[102]

if not, is it possible to handle the panttern with variable toghter ? such as :

I have a variable
str = "101" -- 101 is in variable as it could be 102,103,104 etc. as text above.

if I want to get something concerned with 101, then I need to get position of first [101] and the second [101], then I can get the texts btw them . also I can get others btw 2*[101]
(for this target, I hv to add too many [101][102][103][104] in the text file, then I can get what I want. )

to get [101] [102][103][104], I need to make a pattern. but I tried many way, still cannot put the variable name / the str in pattern. is it impossible ?

(acturally I already solved this by my way. I cut all texts into different txt files. then I need not put so many [102][103][104] etc in text. also good for my classes. but I found above and still want to solve for later. )

thanks much for your help.

Comments

  • dave1707dave1707 Mod
    Posts: 7,468

    @totorofat To read a text file, use something like myString = readText("Documents:MyFile"). Then to select specific things from myString use
    string.gmatch() using patterns for what you want from the file. See the build in reference for string.gmatch and Patterns overview.

  • yes dave . I used that and those.

  • RdoRdo
    edited October 2017 Posts: 23

    @totorofat You want to look up Lua. Codea is atleast in part built on the back of Lua coding.

    In fact some of the patterns/string reference is even echoed in the Lua reference.

  • em2em2
    Posts: 194

    io is used for reading files directly from the file system. Codea has a Documents folder from where you can io.read and io.write.

    https://www.lua.org/manual/5.1/manual.html#5.7

    If the actual section doesn't show up, scroll down to point 5.7

  • @em2 thanks for ur reply. Io.line is good tool for txt file. But you see that the file address is not as normal in text. Readtext("documents:ss.txt") is used in codea. But io.line("documents:ss.txt") cannot work.. as the format of address should be different. I cannot find how to write the address in io.line() . Pls help.

  • dave1707dave1707 Mod
    Posts: 7,468

    @totorofat Here is what I used to use for file io. I don’t know if these still work because I now use readText for any text file.

    file = os.getenv("HOME").."/Documents/filename.txt" 
    
    rFd = io.open(file,"r")
    
    data=rFd:read()
    
    rFd:close()
    
  • edited October 2017 Posts: 46

    @dave1707 so many tears are flying across my face !!!
    that works greatly and line() can work well.

    i=1
    for line in io.lines(file) do
    print (i)
    print(line)
    i=i+1
    

    above is better to use in coding.

    appreciate again !!! I need to go to wash face.

Sign In or Register to comment.