1. print and get procedures

The idea is to do print() as dump of a sequence and with get() do a readback of that sequence .
Oke that works as long as it is only one sequence .
But this :
print(fileout,seq1)
print(fileout,seq2)
print(fileout,seq3)
Save this three sequence's to fileout .
But how to get thim back ?
seq1=get(filein) - works
seq2=get(filein) - doesn't work
seq3=get(filein) - doesn't work

Why , because the GET() routine within get.e does a recursion which ends on a wrong character .

I changed it in this way :

function Get(integer l) --l is level counter 
-- read a Euphoria data object as a string of characters 
-- and return {error_flag, value} 
-- Note: ch is "live" at entry and exit of this routine 
    sequence s, e 
    skip_blanks() 
    if find(ch, START_NUMERIC) then 
        return get_number() 
    elsif ch = '{' then 
        -- process a sequence 
        s = {} 
        get_ch() 
        skip_blanks() 
        if ch = '}' then 
            if l then get_ch() end if  -- change into if statement 
            return {GET_SUCCESS, s} -- empty sequence 
        end if     
        while TRUE do 
            e = Get(l+1) -- read next element  add level 
            if e[1] != GET_SUCCESS then 
                return e 
            end if 
            s = append(s, e[2]) 
            skip_blanks() 
            if ch = '}' then 
                if(l) then get_ch() end if  -- change into if statement 
                return {GET_SUCCESS, s} 
            elsif ch != ',' then 
                return {GET_FAIL, 0} 
            end if 
            get_ch() -- skip comma 
        end while 
    elsif ch = '\"' then 
        return get_string() 
    elsif ch = '\'' then 
        return get_qchar() 
    elsif ch = -1 then 
        return {GET_EOF, 0} 
    else 
        return {GET_FAIL, 0} 
    end if 
end function 

change the call to GET() into GET(0)

new topic     » topic index » view message » categorize

2. Re: print and get procedures

This works for me:

include get.e 
 
sequence seq1 = "abc", seq2 = "def", seq3 = "ghi" 
 
atom fn = open("Test.txt","w") 
 
print(fn,seq1) 
puts(fn,' ')  --white space necessary 
print(fn,seq2) 
puts(fn,'\n')  --white space 
print(fn,seq3) 
 
close(fn) 
 
fn = open("Test.txt","r") 
 
sequence  
s1 = get(fn), 
s2 = get(fn), 
s3 = get(fn) 
 
print(1,s1) 
puts(1,'\n') 
print(1,s2) 
puts(1,'\n') 
print(1,s3) 
new topic     » goto parent     » topic index » view message » categorize

3. Re: print and get procedures

I changed get.e to:

function Get() 
-- read a Euphoria data object as a string of characters 
-- and return {error_flag, value} 
-- Note: ch is "live" at entry and exit of this routine 
    sequence s, e 
    skip_blanks() 
    if find(ch, START_NUMERIC) then 
	return get_number() 
    elsif ch = '{' then 
	-- process a sequence 
	s = {} 
	get_ch() 
	skip_blanks() 
	if ch = '}' then 
--	    get_ch()          -- removed      
	    return {GET_SUCCESS, s} -- empty sequence 
	end if 
	while TRUE do 
	    e = Get() -- read next element 
	    if e[1] != GET_SUCCESS then 
		return e 
	    end if 
	    s = append(s, e[2]) 
	    skip_blanks() 
	    if ch = '}' then 
--		get_ch()          -- removed   
		return {GET_SUCCESS, s} 
	    elsif ch != ',' then 
		return {GET_FAIL, 0} 
	    end if 
	    get_ch() -- skip comma 
	end while 
    elsif ch = '\"' then 
	return get_string() 
    elsif ch = '\'' then 
	return get_qchar() 
    elsif ch = -1 then 
	return {GET_EOF, 0} 
    else 
	return {GET_FAIL, 0} 
    end if 
end function 
 
new topic     » goto parent     » topic index » view message » categorize

4. Re: print and get procedures

cargoan said...

I changed get.e to:

-- Note: ch is "live" at entry and exit of this routine 

That's the problem. ch is "live" on exit but the first thing the next get(fn) does is get_ch().

cargoan said...
--	    get_ch()          -- removed      
--		get_ch()          -- removed   

Without testing, I suspect you were closer on the first attempt, and that this will fail on nested sequences, eg { {}, {1} }.

HTH,
Pete

new topic     » goto parent     » topic index » view message » categorize

5. Re: print and get procedures

Yes, I solved with:

In get.e

... 
char ch  -- the current character 
... 
changed to: 
... 
char ch  -- the current character 
ch = GET_EOF 
... 
 
... 
global function get(integer file) 
-- Read the string representation of a Euphoria object  
-- from a file. Convert to the value of the object. 
-- Return {error_status, value}. 
	input_file = file 
	input_string = 0 
	get_ch() 
	return Get() 
end function 
 
global function value(sequence string) 
-- Read the representation of a Euphoria object 
-- from a sequence of characters. Convert to the value of the object. 
-- Return {error_status, value). 
	input_string = string 
	string_next = 1 
	get_ch() 
	return Get() 
end function 
 
changed to: 
 
global function get(integer file) 
-- Read the string representation of a Euphoria object  
-- from a file. Convert to the value of the object. 
-- Return {error_status, value}. 
	input_file = file 
	input_string = 0 
	if ch = GET_EOF then 
		get_ch() 
	end if 
	return Get() 
end function 
 
global function value(sequence string) 
-- Read the representation of a Euphoria object 
-- from a sequence of characters. Convert to the value of the object. 
-- Return {error_status, value). 
	input_string = string 
	string_next = 1 
	if ch = GET_EOF then 
		get_ch() 
	end if 
	return Get() 
end function 

In std/get.e

... 
char ch  -- the current character 
... 
to: 
... 
char ch = GET_EOF -- the current character 
... 
 
function Get2() 
... 
	-- init 
	offset = string_next-1 
	get_ch() 
to 
	-- init 
	offset = string_next-1 
	-- get_ch() 
... 
 
function get_value(object target, integer start_point, integer answer_type) 
... 
	if answer_type = GET_SHORT_ANSWER then 
		get_ch() 
	end if 
	return call_func(answer_type, {}) 
end function 
 
to:  
	if ch = GET_EOF then 
		get_ch() 
	end if 
	return call_func(answer_type, {}) 
end function 

Seem it's works.

new topic     » goto parent     » topic index » view message » categorize

6. Re: print and get procedures

menno said...

The idea is to do print() as dump of a sequence and with get() do a readback of that sequence .
Oke that works as long as it is only one sequence .
But this :
print(fileout,seq1)
print(fileout,seq2)
print(fileout,seq3)
Save this three sequence's to fileout .
But how to get thim back ?
seq1=get(filein) - works
seq2=get(filein) - doesn't work
seq3=get(filein) - doesn't work

Why , because the GET() routine within get.e does a recursion which ends on a wrong character .

I changed it in this way :

function Get(integer l) --l is level counter 
-- read a Euphoria data object as a string of characters 
-- and return {error_flag, value} 
-- Note: ch is "live" at entry and exit of this routine 
    sequence s, e 
    skip_blanks() 
    if find(ch, START_NUMERIC) then 
        return get_number() 
    elsif ch = '{' then 
        -- process a sequence 
        s = {} 
        get_ch() 
        skip_blanks() 
        if ch = '}' then 
            if l then get_ch() end if  -- change into if statement 
            return {GET_SUCCESS, s} -- empty sequence 
        end if     
        while TRUE do 
            e = Get(l+1) -- read next element  add level 
            if e[1] != GET_SUCCESS then 
                return e 
            end if 
            s = append(s, e[2]) 
            skip_blanks() 
            if ch = '}' then 
                if(l) then get_ch() end if  -- change into if statement 
                return {GET_SUCCESS, s} 
            elsif ch != ',' then 
                return {GET_FAIL, 0} 
            end if 
            get_ch() -- skip comma 
        end while 
    elsif ch = '\"' then 
        return get_string() 
    elsif ch = '\'' then 
        return get_qchar() 
    elsif ch = -1 then 
        return {GET_EOF, 0} 
    else 
        return {GET_FAIL, 0} 
    end if 
end function 

change the call to GET() into GET(0)

if all you need is to write and read seq's try this

include std/io.e sequence seq, seq1 = "abc", seq2 = "def", seq3 = "ghi"

write_lines("Test.txt",{seq1,seq2,seq3})

seq = read_lines("Test.txt") puts(1,seq[1]) puts(1,'\n') puts(1,seq[2]) puts(1,'\n') puts(1,seq[3])

new topic     » goto parent     » topic index » view message » categorize

7. Re: print and get procedures

A possible solution:

In get.e and std/get.e

... 
constant DIGITS = "0123456789", 
	 HEX_DIGITS = DIGITS & "ABCDEF", 
	 START_NUMERIC = DIGITS & "-+.#", 
	-- Added - Valid start characters for an euphoria object 
	 VALID_START = START_NUMERIC & {'{','\"','\''} 
... 
char ch  -- the current character 
ch = GET_EOF  -- Added - Initialize to an invalid start character 
... 

In get.e

global function get(integer file) 
... 
    input_file = file 
    input_string = 0 
    if not find(ch, VALID_START) then	-- added to preserve ch if start character 
	get_ch() -- discards only if ch is an invalid start character (separator) 
    end if 
    return Get() 
end function 
 
global function value(sequence string) 
... 
    input_string = string 
    string_next = 1 
    if not find(ch, VALID_START) then	-- added - preserve ch if is a start character 
	get_ch() -- discards only if ch is an invalid start character (separator) 
    end if 
    return Get() 
end function 

In std/get.e

function Get2() 
... 
    -- init 
    offset = string_next-1 
--  get_ch()  -- comment out, same behavior as Get() 
... 
 
function get_value() 
... 
-- Why different behavior for Get and Get2?, removed if (see Get2) 
--  if answer_type = GET_SHORT_ANSWER then 
    if not find(ch, VALID_START) then	-- added - preserve ch if is a start character 
        get_ch() -- discard only if ch is an invalid start character (separator) 
    end if 
    return call_func(answer_type, {}) 
end function 

It's correct?

new topic     » goto parent     » topic index » view message » categorize

8. Re: print and get procedures

Hi

There is an excellent library for bget and bprint

http://www.rapideuphoria.com/cgi-bin/asearch.exu?dos=on&win=on&lnx=on&gen=on&keywords=bget

this dumps and retrieves sequences, works with eu 4 euiw, uses the 3.1 includes, and is pretty fast.

(remove tick rate from the top of the demo programs)

Chris

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu