New Readtext function

hello jmv38 has written

function readTest(file)
    print("------------------------ ")
    print("read from "..file)
    io.input(io.open(file,"r"))
    local li
    for i in io.lines() do print(i) end
    -- no close needed: done by io.lines()
    print("---- read done ----")
end

with io functions and io.lines()

but now we have readtext but how can i use it
to read word by word and line by line and save words by example in tab

@simeon write new goto statement ( is it BASIC GOTO ? )

thanks

@hpsoft The easiest is to read the text file into a string and parse the string word by word. I don’t have any right now, but I’ll look.

@hpsoft Here’s a small example. You would do a readText into str. I have str with just garbage words.

function setup()
    str="qwe rty uio bgt y123gf vgt cdf vfyhh gre cfrr"
    for z in string.gmatch(str,"%g+") do
        print(z)
    end
end

thanks @dave1707 for fast answer
does it works with carriage return too ?

it s works with carriage return too but not for special ascii

for example with this string or text

«
? n «
IF n 1 ?
THEN n
ELSE
0 1
2 n START
DUP ROT +
NEXT
SWAP
DROP
END
»
»

@hpsoft Here’s another way, probably not the greatest, but it works.

function setup()
    str="« ? n « IF n 1 ? THEN n ELSE 0 1 2 n START DUP ROT + NEXT SWAP DROP END » »"
    a=""
    for z=1,string.len(str) do
        v=string.sub(str,z,z)
        if v==" " then
            print(a)
            a=""
        else
            a=a..v
        end
    end
end

same with gmatch and a pattern

function setup()
    str=[[qwe rty uio bgt y123gf vgt cdf vfyhh gre cfrr
    « ? n « IF n 1 ? THEN n ELSE 0 1 2 n START DUP ROT + NEXT SWAP DROP END » »
    ]]
    for z in string.gmatch(str,"(.-)[%s]+") do
        print(z)
    end
end

[edit] modified the pattern

patterns are like a magic esoteric langage. Completely obfuscated, until you start to get it. Then it becomes really cool. Experliamus!

very good solution

they do appear in my version

@Jmv38 You can eliminate the [ ] around %s . The square brackets are for a set and you only have 1 item in it. You were probably trying multiple items in the set and realized the %s was enough but didn’t remove the [ ] .

@Dave1707 correct guess!

    for z in string.gmatch(str,"(.-)%s+") do
        print(z)
    end

thanks @Jmv38 and @Dave1707
for z in string.gmatch(str…" “,”(.-)%s+") do
print(z)
end
i add " " to have the last item
perhaps it was interesting to create a function to translate
string.gmatch with very c++ very complicated syntax
by very simplified lua function
if i have the time

for anyone the final cut

function spaceSplit(str)
    str = str .." "
    local out = {}
    for z in string.gmatch(str,"(.-)%s+") do
        table.insert(out,z)
    end
    return out
end

another more compact way if you dont need the table output

function setup()
    str=[[qwe rty uio bgt y123gf vgt cdf vfyhh gre cfrr
    « ? n « IF n 1 ? THEN n ELSE 0 1 2 n START DUP ROT    + NEXT SWAP DROP END » »
]]
    for s in spaceSplit(str) do print(s) end
end

function spaceSplit(str)
    str = str .." "
    return string.gmatch(str,"(.-)%s+") 
end

some more?

function setup()
    str = "12-add--FGv"
    for s in charSplit(str,"-") do print(s) end
    
    str = "12|add|FGv"
    for s in charSplit(str,"|") do print(s) end
end
function charSplit(str,c)
    str = str ..c
    return string.gmatch(str,"(.-)"..c.."+") 
end

Very interesting

a small code for test

function setup()
    
    ascii={"«","»","?","?","?","¬","?","×","?","?","?","^","“","‘","œ","?"}
    
    displayMode(OVERLAY) ; textMode(CORNER)   
    StrDep=readText("Project:Fib2")
    
    parameter.boolean("letters",false)
    parameter.boolean("control_characters",false)
    parameter.boolean("digits",false)
    parameter.boolean("printable_characters_except_space",false)
    parameter.boolean("lowercase_letters",false)
    parameter.boolean("punctuation_characters",false)
    parameter.boolean("space_characters",false)
    parameter.boolean("uppercase_letters",false)
    parameter.boolean("alphanumeric_characters",false)
    parameter.boolean("hexadecimal_digits",false)
    parameter.boolean("alphanumeric_characters_plus_underscore",false)
    parameter.boolean("octal_digits",false)
    parameter.boolean("union",false)
    
end

function draw()
    background(41, 28, 140, 255) ; fill(241, 255, 0, 255)
    fontSize(25)
    set=""
    if union then set=set.."[" end
    if octal_digits then set=set.."0-7" end
    if letters then set=set.."%a" end
    if control_characters then set=set.."%c" end
    if digits then set=set.."%d" end
    if printable_characters_except_space then set=set.."%g" end
    if lowercase_letters then set=set.."%l" end
    if punctuation_characters then set=set.."%p" end
    if space_characters then set=set.."%s" end
    if uppercase_letters then set=set.."%u" end
    if alphanumeric_characters then set=set.."%w" end
    if hexadecimal_digits then set=set.."%x" end
    if alphanumeric_characters_plus_underscore then set=set.."%w_" end
    if union then set=set.."]" end
    text("set="..set,340,700)
    AffStrDep()
    CreateTab()
    AffStrFin() 
end

function AffStrDep()
    ligne=1
    for i=1, string.len(StrDep), 100 do
        text(string.sub(StrDep,i,i+100),340,400-ligne*80)
        ligne=ligne+1
    end
end

function CreateTab()
    StrFin=""
    tab={} ; i=1
    for z in string.gmatch(StrDep,set) do
        tab[i]=z
        StrFin=StrFin..z.." "
    end
end

function AffStrFin()
    ligne=1
    for i=1, string.len(StrFin), 100 do
        text(string.sub(StrFin,i,i+100),600,400-ligne*80)
        ligne=ligne+1
    end
end