1. A function that expands value() and more

Hi, It seems that I was finally approved to post on this forum - although I haven't received any confirmation e-mail.
I thought of using Euphoria on a new project due to its (claimed) capacity to easily translate to C.
However, without help and support it may take too long to learn and evaluate its adequacy.
I like to share when I'm learning. I came across a text that states the limitations of the value() built-in function.
I thought it would not be too much difficult to improve it in order to get numbers from strings independently of their position.
Here's what I could do as a beginner (pelease, do feel free to make corrections):

include get.e 
function val(object parm) 
    -- Gets any numerical value at any position on a string 
    sequence num_val = {} 
    if sequence(parm) then 
        for i = 1 to length(parm) do 
            if parm[i] >= 48 and parm[i] <= 57 then 
                num_val = append(num_val, parm[i]) 
            end if 
        end for 
        num_val = value(num_val) 
        return num_val[2] 
    else 
        return 0 
    end if 
end function 
--tests: 
print(1, val("This string includes the number 42.")) 
puts(1, "\n") 
print(1, val("This string doe not include any number.")) 
puts(1, "\n") 
print(1, val("We can extract number 42 from this string and add two.") + 2) 

I also wanted to ask an Euphorian that may have the requested reputation to create an Euphoria tag and leave some answer here:
https://stackoverflow.com/users/7834761/j-partridge
(I'm using ed with a file myshell and the only thing missing is the > before the prompt... while writing the ex files on wee.) :)

new topic     » topic index » view message » categorize

2. Re: A function that expands value() and more

Welcome to OE.

There is no interactive shell for Euphoria. We do not run individual statements from a command line in the Python style.

It is much easier to get an answer on Euphoria from the forum than stackoverflow.

Nothing wrong with your code.

Let's call it p00.ex. I test on Linux.

Compiling gives me:

euc p00.ex 
--> p00 
-- 200 kB 

Binding gives me:

eubind p00.ex 
--> p00 
-- 2.1 Mb 

Binding is an interpreter and code all-in-one.

One coding trick that is available is:

... 
            if parm[i] >= '0' and parm[i] <= '9' then 
... 

Think of plain text and numbers in Euphoria as the same thing.


Another option is to look a Phix. This is another way to write Euphoria code.

_tom

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

3. Re: A function that expands value() and more

Welcome to the forum!

Just an observation. The function does not "Gets any numerical value at any position on a string", but rather gets or extracts the first numerical value found in the string and appends any subsequent numerical symbols to the initial numerical value.

print(1, val("This string includes the numbers 42 18 36."))  
-- outputs 
-- 421816 instead of 
-- 42 18 16 

Regards,
Ken Rhodes

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

4. Re: A function that expands value() and more

Senator said...

Just an observation. The function does not "Gets any numerical value at any position on a string", but rather gets or extracts the first numerical value found in the string and appends any subsequent numerical symbols to the initial numerical value.

print(1, val("This string includes the numbers 42 18 36."))  
-- outputs 
-- 421816 instead of 
-- 42 18 16 

So, the function should output a sequence of found values. In the case above, it would return {42,18,36}.

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

5. Re: A function that expands value() and more

Tanks _tom and Senator.

You're right Senator. I'll change the comment to "...the first numerical value found..."

What would be your suggestion to get the right output? I'm willing to learn.

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

6. Re: A function that expands value() and more

I once made a start on a repl for Phix and just spent the last 10 minutes resurrecting that, so it is available on my machine, but not uploaded anywhere yet.
However it is severely limited, one-shot fashion, eg you cannot declare a variable and then set it, or declare a routine and then call it!

You can do things like this (well, I mean, I can, if you try this you'll immediately get a 9/0 error):

p -repl 
>?3+5 
8 
>?"hello" 
"hello" 
>q 

Very little use to anyone really, but I guess it's a start...

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

7. Re: A function that expands value() and more

euphoric said...
Senator said...

Just an observation. The function does not "Gets any numerical value at any position on a string", but rather gets or extracts the first numerical value found in the string and appends any subsequent numerical symbols to the initial numerical value.

print(1, val("This string includes the numbers 42 18 36."))  
-- outputs 
-- 421816 instead of 
-- 42 18 16 

So, the function should output a sequence of found values. In the case above, it would return {42,18,36}.

You can do this in Phix:

string s = "This string includes the numbers 42 18 36." 
sequence res = scanf(" "&s,"%s %d%s") 
pp(res,{pp_StrFmt,-2}) 
?vslice(res,2) 

Output:

{{" This string includes the numbers", 42, " 18 36."}, 
 {" This string includes the numbers 42", 18, " 36."}, 
 {" This string includes the numbers 42 18", 36, "."}} 
{42,18,36} 
new topic     » goto parent     » topic index » view message » categorize

8. Re: A function that expands value() and more

Thanks petelomax. It's good to know that someone else also misses the single guillemet. :) :)

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

9. Re: A function that expands value() and more

petelomax said...
euphoric said...
Senator said...

Just an observation. The function does not "Gets any numerical value at any position on a string", but rather gets or extracts the first numerical value found in the string and appends any subsequent numerical symbols to the initial numerical value.

print(1, val("This string includes the numbers 42 18 36."))  
-- outputs 
-- 421816 instead of 
-- 42 18 16 

So, the function should output a sequence of found values. In the case above, it would return {42,18,36}.

You can do this in Phix:

string s = "This string includes the numbers 42 18 36." 
sequence res = scanf(" "&s,"%s %d%s") 
pp(res,{pp_StrFmt,-2}) 
?vslice(res,2) 

Output:

{{" This string includes the numbers", 42, " 18 36."}, 
 {" This string includes the numbers 42", 18, " 36."}, 
 {" This string includes the numbers 42 18", 36, "."}} 
{42,18,36} 

Your code sure seems nice, euphoric. But you didn't use Euphoria, you used another language...
You literally "Phixed" it, right? :)
I wrote a script that gets the output mentioned by Senator.

include get.e 
include std/sequence.e 
function val(object parm) 
    -- Gets any numerical value at any position on a string 
    sequence num_val = {} 
    sequence numbers = "0123456789" 
    if sequence(parm) then 
        for i = 1 to length(parm) do             
            if find(parm[i], numbers) then 
                num_val = append(num_val, {i, parm[i]}) 
            end if 
        end for         
        sequence fnum_val = {} 
        fnum_val = flatten(num_val)         
        sequence values = {} 
        atom j = 3 
        while j <= length(fnum_val) do         
            atom dif = fnum_val[j] - fnum_val[j-2] 
            if dif != 1 then 
                values = append(values, fnum_val[j-1]) 
                values = append(values, 44) 
            else 
                values = append(values, fnum_val[j-1]) 
            end if 
            j = j + 2 
        end while 
        atom k = length(fnum_val) 
        values = append(values, fnum_val[k])         
        return values 
    else 
        return 0 
    end if     
end function 
 
puts(1, val("This string includes numbers 42 52 and 33.")) 

outputs 42,52,33

However, I doubt that has been evaluated to numerical... Has it? Can they be used as numerical values? I don't know, I've been testing Euphoria only for 10 days or so. Please, be patient with me and keep answering my (eventually dumb) questions. Okay? Thanks.

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

10. Re: A function that expands value() and more

jpartridge said...
petelomax said...
euphoric said...
Senator said...

Just an observation.

So, the function should output a sequence of found values. In the case above, it would return {42,18,36}.

You can do this in Phix:

string s = "This string includes the numbers 42 18 36." 
sequence res = scanf(" "&s,"%s %d%s") 
pp(res,{pp_StrFmt,-2}) 
?vslice(res,2) 

Output:

Your code sure seems nice, euphoric.

Not my code! grin

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

11. Re: A function that expands value() and more

jpartridge said...

However, I doubt that has been evaluated to numerical... Has it? Can they be used as numerical values? I don't know, I've been testing Euphoria only for 10 days or so. Please, be patient with me and keep answering my (eventually dumb) questions. Okay? Thanks.

Basically, your output must be a string. A sequence stores no semantics of how it must be displayed. If you use puts() the second argument will be treated as a string. If you use print(), it will be treated as a numeric sequence. Puts() cannot tell whether what you give it is meant to be a string or a sequence of numbers.

in particular, 48='0' but if you're building a string you ought to use '0' rather than 48 just for clarity of your intentions.

There are other ways you can learn Euphoria. Read our manual. We have html and pdf! It's a lot to take in at once. Check out the demos, and try making small changes in a copy of them. If you get unexpected results, come ask here. People are friendly.

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

Search



Quick Links

User menu

Not signed in.

Misc Menu