1. text to number 2

ok,  I have reviewed the value library function for converting a 
sequence to a number,  but how do you get a sequence like 
{"-","1","2",".","2"}  to be {"-12.2"} as the function needs it to be?  
Or am I missing something obvious?  I need to make a file filter and I 
need to be able to convert back and forth regularly from string to 
numeric.

new topic     » topic index » view message » categorize

2. Re: text to number 2

Try this:

sequence original, final
original = {"-","1","2",".","2"}
final= {}

for n = 1 to length(original) do
  final &= original[n]
end for

puts (1, final) 

Dan Moyer

----- Original Message ----- 
From: <dmccu at connect.ab.ca>
To: "EUforum" <EUforum at topica.com>
Sent: Sunday, January 27, 2002 8:58 PM
Subject: text to number 2


> 
> ok,  I have reviewed the value library function for converting a 
> sequence to a number,  but how do you get a sequence like 
> {"-","1","2",".","2"}  to be {"-12.2"} as the function needs it to be?  
> Or am I missing something obvious?  I need to make a file filter and I 
> need to be able to convert back and forth regularly from string to 
> numeric. 
> 
> 
> 
>

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

3. Re: text to number 2

On 28 Jan 2002, at 4:58, dmccu at connect.ab.ca wrote:

> 
> ok,  I have reviewed the value library function for converting a 
> sequence to a number,  but how do you get a sequence like 
> {"-","1","2",".","2"}  to be {"-12.2"} as the function needs it to be?  
> Or am I missing something obvious?  I need to make a file filter and I 
> need to be able to convert back and forth regularly from string to 
> numeric. 

 {"-","1","2",".","2"} is a nested sequence.
"-12.2" ( or {-12.2} if you like) is what you need to val() with.

Simply get the -12.5 as "-12.5" to start with, or make a pass thru the nested 
sequence, adding the pieces to one un-nested sequence, then val(un-nested 
sequence).

Kat

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

4. Re: text to number 2

dmc wrote:
> ok, I have reviewed the value library function for converting a 
> sequence to a number, but how do you get a sequence like 
> {"-","1","2",".","2"} to be {"-12.2"} as the function needs it to be? 
> Or am I missing something obvious? I need to make a file filter and I 
> need to be able to convert back and forth regularly from string to 
> numeric. 

The question no one has asked is: How are you getting 
that strange sequence in the first place? 

You mention file filters, so I'm guessing you're reading 
them from files. If so, you're just going about that in 
the wrong way. 

If the files are pure numeric, then get() should work, no 
conversion necessary.

If the files are mixed text and numbers, then
you can either slice the numbers out, i.e. line[12..16]
if they are column-aligned, or if the numbers appear in 
random places within a line, use gets() to read in the line,
and then parse the number(s) out with strtok.s, then convert 
with value()

A sample of the input data would help.

Regards,
Irv

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

5. Re: text to number 2

On Thursday 31 January 2002 01:04 am, you wrote:
>
> Irv,
>
> What is strtok.s?

Strtok.e is an "include" file written by Gabriel Boehm 
and Kat, which (among many other things) breaks strings 
down into units based on a separator token, such as spaces 
or ':' or whatever.

> I haven't heard of that before.  Yes there are numbers mixed in with the
> text at random places and I have to extract the numbers from the text
> file I read in eg X3.5476Y5.0776  I have to take the numbers out and do
> math calculations on them in some cases,  then return them.

Below is a routine that will extract numbers per your example.
This is not a general purpose routine. This illustrates *one* 
way, but more info on the actual format of the data would be 
needed before we could come up with a workable scheme.
Do the numbers all run together like your example? Are 
there always fixed markers such as X and Y? Can the input
contain other words that might throw off the parsing by 
having X or Y in them? 

Also, be aware that you probably won't be able to "return"
values to a text file - you'll have to write the output to 
a new file.

include get.e
atom fn
object Xval, Yval
integer xp, yp
object line

fn = open("test.data","r")
line = gets(fn)
xp = match("X",line)
yp = match("Y",line)
Xval = value(line[xp+1..yp-1])
Yval = value(line[yp+1..length(line)])

if Xval[1] != GET_SUCCESS then
   puts(1,"Error evaluating X")
else Xval = Xval[2]
     ? Xval
end if
if Yval[1] != GET_SUCCESS then
   puts(1,"Error evaluating Y")
else Yval = Yval[2]
     ? Yval
end if

Regards,
Irv

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

Search



Quick Links

User menu

Not signed in.

Misc Menu