1. how get "number" in disk text file read into integer variable?

Ok gang, here's my problem now <sigh!>:

How do I get a "number" I have saved into a (text) disk file back as an
integer?

I'm trying to save a structured variable to a disk file & then be able to
read it from the file & put it back  into the structured variable.  I'm
using the
"newex06d.exw" database example by Ad Rienks from the Tutorial as a starting
point, & proceeding tiny steps at a time.

My first step was to make a text file that would LOOK like the saved file
should look like, and then just READ that file into my structured variable.
Since part of my structured variable is of variable length, I put that
length into a disk file (using a text editor), so it can be read out & used
to count lines in the file to know what to put where in the structured
variable.

So if my first variant length is "3", I put the number 3 into the disk
file on a line; but I can't seem to get that "3" back as an integer when I
read it (I get 51, which is ASCII for 3).  I'm guessing this is
pathetically simple, but I can't get it.

Dan Moyer

new topic     » topic index » view message » categorize

2. Re: how get "number" in disk text file read into integer variable?

On Fri, 21 Jan 2000 23:10:14 -0800, Dan B Moyer <DANMOYER at PRODIGY.NET>
wrote:

>Ok gang, here's my problem now <sigh!>:
>
>How do I get a "number" I have saved into a (text) disk file back as an
>integer?
>
>I'm trying to save a structured variable to a disk file & then be able to
>read it from the file & put it back  into the structured variable.  I'm
>using the
>"newex06d.exw" database example by Ad Rienks from the Tutorial as a
starting
>point, & proceeding tiny steps at a time.
>
>My first step was to make a text file that would LOOK like the saved file
>should look like, and then just READ that file into my structured variable.
>Since part of my structured variable is of variable length, I put that
>length into a disk file (using a text editor), so it can be read out & used
>to count lines in the file to know what to put where in the structured
>variable.
>
>So if my first variant length is "3", I put the number 3 into the disk
>file on a line; but I can't seem to get that "3" back as an integer when I
>read it (I get 51, which is ASCII for 3).  I'm guessing this is
>pathetically simple, but I can't get it.
>
>Dan Moyer



try this:

sequence mySeq
integer myInt

myInt = 3
mySeq = sprintf("%d", myInt)

-- now mySeq = {"3"}

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

3. Re: how get "number" in disk text file read into integer variable?

I guess I should read the whole question before I reply. I think I answered
you question backwards.

To get an integer from a sequence, use:

include get.e

sequence mySeq
sequence temp
integer myInt

mySeq = "3"  -- same as {51}

temp = value(mySeq)

-- temp[1] = GET_SUCCESS (which means that value worked)
-- temp[2] = 3

myInt = temp[2]

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

4. Re: how get "number" in disk text file read into integer variable?

Brian,

I did try something almost exactly like that, with the single(?) difference
that the "3" was being read from a line of a file into an object variable
instead of being defined in a sequence; in my case, I get the error:

 "C:\EUPHORIA\INCLUDE\get.e:301 in function value()
type_check failure, string is 51'3'
    string = 51'3'

Any idea what I might be doing wrong?

Dan

Brian wrote:


>I guess I should read the whole question before I reply. I think I answered
>you question backwards.
>
>To get an integer from a sequence, use:
>
>include get.e
>
>sequence mySeq
>sequence temp
>integer myInt
>
>mySeq = "3"  -- same as {51}
>
>temp = value(mySeq)
>
>-- temp[1] = GET_SUCCESS (which means that value worked)
>-- temp[2] = 3
>
>myInt = temp[2]

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

5. Re: how get "number" in disk text file read into integer variable?

Brian,

Oops! It must be catching!  The error I sent you was NOT from my "nearly
like what you said" code, but from an alteration of it I made trying to get
it to work; my "nearly like what you said" code runs, but still returns the
value "51" instead of "3" when it reads in a "3" from a disk file.

What I have from the Tutorial example plus my modification is:

object line
atom Handle
sequence temp
integer NumOfLines

Handle = open("phrase.dat", "r")     -- open the file,
 line = gets(Handle)

  temp = value(line[2])-- line of text actually has a marker before the , so
I get the 2nd item on the line
NumOfLines = temp[2]

but what I get for NumOfLines is 51, not 3.

Any idea why?

Dan



Brian wrote:


>I guess I should read the whole question before I reply. I think I answered
>you question backwards.
>
>To get an integer from a sequence, use:
>
>include get.e
>
>sequence mySeq
>sequence temp
>integer myInt
>
>mySeq = "3"  -- same as {51}
>
>temp = value(mySeq)
>
>-- temp[1] = GET_SUCCESS (which means that value worked)
>-- temp[2] = 3
>
>myInt = temp[2]

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

6. Re: how get "number" in disk text file read into integer variable?

Brian,

Jeeze, this is what I get for using spell check!  When I said "line of text
actually has a marker before the , so I get the 2nd item on the line", what
I MEANT was:
"line of text actually has a marker before the NUMBER, so I get the 2nd item
on the line".

Corrected code below:

What I have from the Tutorial example plus my modification is:

object line
atom Handle
sequence temp
integer NumOfLines

Handle = open("phrase.dat", "r")     -- open the file,
line = gets(Handle)

  temp = value(line[2])-- line of text in file actually has a marker before
the number, so
I get the 2nd item on the line
NumOfLines = temp[2]

but what I get for NumOfLines is 51, not 3.

Any idea why?

Dan

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

7. Re: how get "number" in disk text file read into integer variable?

Brian,

If I understand correctly, when "value" looks at the number 3 my code reads
from a disk file, it SEES {51} (which is ASCII for 3), which it then
evaluates as the number 51?  This is why I originally thought I needed
something different than "value" to return the number correctly.  Now I'm
wondering if I WROTE the number 3 to a disk file, instead of putting it
there with a text editor, it would be in some different representation that
would allow subsequent reads to get it correctly?


-- code:
-- file to be read was created with text editor, has a marker followed by 3
on a line
object line
atom Handle
sequence temp
integer NumOfLines

Handle = open("phrase.dat", "r")     -- open the file,
line = gets(Handle)

  temp = value(line[2])-- line of text in file actually has a marker before
the number 3, so
I get the 2nd item on the line
NumOfLines = temp[2]

but what I get for NumOfLines is 51, not 3.

Dan
(maybe calling it quits for the night)



Brian wrote:
>I guess I should read the whole question before I reply. I think I answered
>you question backwards.
>
>To get an integer from a sequence, use:
>
>include get.e
>
>sequence mySeq
>sequence temp
>integer myInt
>
>mySeq = "3"  -- same as {51}
>
>temp = value(mySeq)
>
>-- temp[1] = GET_SUCCESS (which means that value worked)
>-- temp[2] = 3
>
>myInt = temp[2]

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

8. Re: how get "number" in disk text file read into integer variable?

Hi,

I may be asking you a stupid question, BUT..
How do you know you are getting 51 instead of 3?

I rather recall running into this myself and I 'think' the solution is this:

If you are using printf or sprinf to 'see' the number=value[2] you need to
use printf or sprintf("%s",number) since number is an atom in this sample.
If you use "%d" you always get 51. If number were more than one digit, you
would run into this problem because "%d" would work fine.

My memory is a bit foggy so I really hope this is correct.

Judith Evans

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

9. Re: how get "number" in disk text file read into integer variable?

Oops, forgot a word.

>If number were more than one digit, you
>would NOT run into this problem because "%d" would work fine.
>

Judith

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

10. Re: how get "number" in disk text file read into integer variable?

/me notices the slew of error msgs on the list, all posted after 1am,, so
i'm not the only one who does this!
smile

Kat


----- Original Message -----
From: Dan B Moyer <DANMOYER at PRODIGY.NET>
To: <EUPHORIA at LISTSERV.MUOHIO.EDU>
Sent: Saturday, January 22, 2000 2:04 AM
Subject: Re: how get "number" in disk text file read into integer variable?


> Brian,
>
> Oops! It must be catching!



>
> Brian wrote:
>
>
> >I guess I should read the whole question before I reply. I think I
answered
> >you question backwards.

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

11. Re: how get "number" in disk text file read into integer variable?

Judith,

No, it's not a stupid questions at all; it did make me think  about it a
little more(as well as I can at this time of the night) .  And the answer to
my problem SEEMS to be to simply put the thing I'm taking the "value" of
inside {}'s first.

But to answer your question first, the reason I think I am getting 51
instead of 3 when I am reading a line that I put a 3 into is because things
weren't happening like they should have when I USED the number; that's when
I displayed it, to try to see what might be going wrong, but with wPrint,
not printf or sprinf. (Not sure how much if any difference that makes.)

But reading your question led me to see if there were anything I hadn't
tried yet (or didn't remember trying), & so I did the following, & the
section of code seems to be FUNCTIONING correctly, although my "debug"
display still doesn't show 3 (but it does show 4 instead of 51, so I must be
closer, heh heh heh!).

with "line" being a line read from the file, and the 2nd character in the
line being the number I want to use, I had been doing this:
 temp = value(line[2]),  which didn't give a value that functioned correctly
"downstream";

then I did this instead:
temp = value({line[2]}), and that value DOES work right later in my program
(well, not COMPLETELY right, but that's a completely different problem).

So your question in some fashion prompted me to find the solution, which
makes it anything but stupid.  Thanks.

Dan

Judith asked:


>Hi,
>
>I may be asking you a stupid question, BUT..
>How do you know you are getting 51 instead of 3?
>
>I rather recall running into this myself and I 'think' the solution is
this:
>
>If you are using printf or sprinf to 'see' the number=value[2] you need to
>use printf or sprintf("%s",number) since number is an atom in this sample.
>If you use "%d" you always get 51. If number were more than one digit, you
>would run into this problem because "%d" would work fine.
>
>My memory is a bit foggy so I really hope this is correct.
>
>Judith Evans

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

12. Re: how get "number" in disk text file read into integer variable?

Kat,

No kidding!  And the worst of it is that I think maybe my "correction" was
wrong too; BUT, I also seem to have finally solved my problem, and it's even
later than 1am now!

Dan
(blearily looking at all the funny marks on the cathode ray tube)

Kat observes:


>/me notices the slew of error msgs on the list, all posted after 1am,, so
>i'm not the only one who does this!
>smile
>
>Kat
>
>
>----- Original Message -----
>From: Dan B Moyer <DANMOYER at PRODIGY.NET>
>To: <EUPHORIA at LISTSERV.MUOHIO.EDU>
>Sent: Saturday, January 22, 2000 2:04 AM
>Subject: Re: how get "number" in disk text file read into integer variable?
>
>
>> Brian,
>>
>> Oops! It must be catching!
>
>
>
>>
>> Brian wrote:
>>
>>
>> >I guess I should read the whole question before I reply. I think I
>answered
>> >you question backwards.

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

13. Re: how get "number" in disk text file read into integer variable?

/me notices that there are different time zones around the world, and it's
not always clear from what time zone a msg is coming!
/so your real name is Gertie?

Ad

----- Oorspronkelijk bericht -----
Van: Kat <gertie at ZEBRA.NET>
Aan: <EUPHORIA at LISTSERV.MUOHIO.EDU>
Verzonden: zaterdag 22 januari 2000 12:54
Onderwerp: Re: how get "number" in disk text file read into integer
variable?


> /me notices the slew of error msgs on the list, all posted after 1am,, so
> i'm not the only one who does this!
> smile
>
> Kat
>
>
> ----- Original Message -----
> From: Dan B Moyer <DANMOYER at PRODIGY.NET>
> To: <EUPHORIA at LISTSERV.MUOHIO.EDU>
> Sent: Saturday, January 22, 2000 2:04 AM
> Subject: Re: how get "number" in disk text file read into integer
variable?
>
>
> > Brian,
> >
> > Oops! It must be catching!
>
>
>
> >
> > Brian wrote:
> >
> >
> > >I guess I should read the whole question before I reply. I think I
> answered
> > >you question backwards.

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

14. Re: how get "number" in disk text file read into integer variable?

----- Original Message -----
From: Dan B Moyer <DANMOYER at PRODIGY.NET>
To: <EUPHORIA at LISTSERV.MUOHIO.EDU>
Sent: Saturday, January 22, 2000 8:10 AM
Subject: how get "number" in disk text file read into integer variable?


> Ok gang, here's my problem now <sigh!>:
>
> How do I get a "number" I have saved into a (text) disk file back as an
> integer?
>
> I'm trying to save a structured variable to a disk file & then be able to
> read it from the file & put it back  into the structured variable.  I'm
> using the
> "newex06d.exw" database example by Ad Rienks from the Tutorial as a
starting
> point, & proceeding tiny steps at a time.
>
> My first step was to make a text file that would LOOK like the saved file
> should look like, and then just READ that file into my structured
variable.
> Since part of my structured variable is of variable length, I put that
> length into a disk file (using a text editor), so it can be read out &
used
> to count lines in the file to know what to put where in the structured
> variable.
>
> So if my first variant length is "3", I put the number 3 into the disk
> file on a line; but I can't seem to get that "3" back as an integer when I
> read it (I get 51, which is ASCII for 3).  I'm guessing this is
> pathetically simple, but I can't get it.
>
> Dan Moyer
>

'3'- '0'=3
51-48=3

Or just use this:
-- Begin MMATCH.E --
global function multimatch(sequence str,sequence cstr)
object loc,pl,temp1,temp2,temp3,lngt,t1,t2
 temp1=""
 temp2=cstr
 pl=0
 lngt=length(str)
 loc=match(str,temp2)
  while loc do
   temp3=loc+pl*lngt
   temp1=temp1 & temp3
   t1=temp2[1..loc-1]
   t2=temp2[loc+lngt..length(temp2)]
   temp2=t1 & t2
   loc=match(str,temp2)
   pl+=1
  end while
return temp1
end function
-- End MMATCH.E --
----------------------------
-- Begin SEQ2INT.E --
include mmatch.e

global function isAtom(sequence input)
object returnment,tmp
tmp=0
 for a=1 to length(input) do
  if input[a]>='0' and input[a]<='9' then
   tmp+=1
  elsif input[a]='.' or input[a]='-' then
   tmp+=1
  end if
 end for
 if tmp=length(input) then
  returnment=-1
 else
  returnment=0
 end if
return returnment
end function

-- Works till "9999999999", try "1.5" or "-1.5" it works --
global function seq2int(sequence str)
object result,tmp1,tmp2,divide
divide=1
result=0
if length(str) !=0 then
 tmp1=find('.',str)
 tmp2=find('-',str)
 if tmp1 !=0 then
  divide=power(10,length(str)-tmp1)
  str=str[1..tmp1-1]&str[tmp1+1..length(str)]
 end if
 if tmp2 !=0 then
  divide*=-1
  str=str[1..tmp2-1]&str[tmp2+1..length(str)]
 end if
 result=str[length(str)]-48
 for a=1 to length(str)-1 do
  result+=power(10,length(str)-a)*(str[a]-48)
 end for
 result/=divide
end if
return result
end function
-- End SEQ2INT.E --
--------------------------

I don't have to say 'I hope that this works', because it just does work...

Bye,
PQ
QC

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

15. Re: how get "number" in disk text file read into integer variable?

Nope, sorry Ad, Gertie is the potential Ai's name. I have teased that Gertie
would have her own email addy, so when i changed isps, i figured what the
heck? She is using that name a bit prematurely for sure, since she is still
mostly mirc bot code, but with Eu plugged into mirc now, and slowly moving
the Pascal code into Eu (and dealing with that paradigm shift), i hope by
the end of the year that Gertie will be where i was in Pascal 5 yrs ago,,
but with the future i can see using Eu. She already has better fault
tolerance in Eu, and a much larger database, and is significantly faster
than Pascal was (even tho the pascal ran in dos and used indexing into the
database!). Mirc is being used as a very easily programmable gui and
human/internet interface, and generic mirc bot channel op stuff when on irc.
I can't tell you how nice it is to be able to kill Eu and still have the
gui/internet running with no errors, or kill mirc and still have the core Eu
running.
 smile

Kat
no, RDS and mIRC didn't pay me for this tongue

----- Original Message -----
From: Ad Rienks <kwibus at ZONNET.NL>
To: <EUPHORIA at LISTSERV.MUOHIO.EDU>
Sent: Saturday, January 22, 2000 8:43 AM
Subject: Re: how get "number" in disk text file read into integer variable?


> /me notices that there are different time zones around the world, and it's
> not always clear from what time zone a msg is coming!
> /so your real name is Gertie?
>
> Ad
>
> ----- Oorspronkelijk bericht -----
> Van: Kat <gertie at ZEBRA.NET>
> Aan: <EUPHORIA at LISTSERV.MUOHIO.EDU>
> Verzonden: zaterdag 22 januari 2000 12:54
> Onderwerp: Re: how get "number" in disk text file read into integer
> variable?
>
>
> > /me notices the slew of error msgs on the list, all posted after 1am,,
so
> > i'm not the only one who does this!
> > smile
> >
> > Kat
> >
> >
> > ----- Original Message -----
> > From: Dan B Moyer <DANMOYER at PRODIGY.NET>
> > To: <EUPHORIA at LISTSERV.MUOHIO.EDU>
> > Sent: Saturday, January 22, 2000 2:04 AM
> > Subject: Re: how get "number" in disk text file read into integer
> variable?
> >
> >
> > > Brian,
> > >
> > > Oops! It must be catching!
> >
> >
> >
> > >
> > > Brian wrote:
> > >
> > >
> > > >I guess I should read the whole question before I reply. I think I
> > answered
> > > >you question backwards.
>

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

16. Re: how get "number" in disk text file read into integer variable?

What's this? A chat AI? Is it running on any servers?

Nick
----- Original Message -----
From: Kat <gertie at ZEBRA.NET>
To: <EUPHORIA at LISTSERV.MUOHIO.EDU>
Sent: Sunday, January 23, 2000 9:16 AM
Subject: Re: how get "number" in disk text file read into integer variable?


> Nope, sorry Ad, Gertie is the potential Ai's name. I have teased that
Gertie
> would have her own email addy, so when i changed isps, i figured what the
> heck? She is using that name a bit prematurely for sure, since she is
still
> mostly mirc bot code, but with Eu plugged into mirc now, and slowly moving
> the Pascal code into Eu (and dealing with that paradigm shift), i hope by
> the end of the year that Gertie will be where i was in Pascal 5 yrs ago,,
> but with the future i can see using Eu. She already has better fault
> tolerance in Eu, and a much larger database, and is significantly faster
> than Pascal was (even tho the pascal ran in dos and used indexing into the
> database!). Mirc is being used as a very easily programmable gui and
> human/internet interface, and generic mirc bot channel op stuff when on
irc.
> I can't tell you how nice it is to be able to kill Eu and still have the
> gui/internet running with no errors, or kill mirc and still have the core
Eu
> running.
>  smile
>
> Kat
> no, RDS and mIRC didn't pay me for this tongue
>
> ----- Original Message -----
> From: Ad Rienks <kwibus at ZONNET.NL>
> To: <EUPHORIA at LISTSERV.MUOHIO.EDU>
> Sent: Saturday, January 22, 2000 8:43 AM
> Subject: Re: how get "number" in disk text file read into integer
variable?
>
>
> > /me notices that there are different time zones around the world, and
it's
> > not always clear from what time zone a msg is coming!
> > /so your real name is Gertie?
> >
> > Ad
> >
> > ----- Oorspronkelijk bericht -----
> > Van: Kat <gertie at ZEBRA.NET>
> > Aan: <EUPHORIA at LISTSERV.MUOHIO.EDU>
> > Verzonden: zaterdag 22 januari 2000 12:54
> > Onderwerp: Re: how get "number" in disk text file read into integer
> > variable?
> >
> >
> > > /me notices the slew of error msgs on the list, all posted after 1am,,
> so
> > > i'm not the only one who does this!
> > > smile
> > >
> > > Kat
> > >
> > >
> > > ----- Original Message -----
> > > From: Dan B Moyer <DANMOYER at PRODIGY.NET>
> > > To: <EUPHORIA at LISTSERV.MUOHIO.EDU>
> > > Sent: Saturday, January 22, 2000 2:04 AM
> > > Subject: Re: how get "number" in disk text file read into integer
> > variable?
> > >
> > >
> > > > Brian,
> > > >
> > > > Oops! It must be catching!
> > >
> > >
> > >
> > > >
> > > > Brian wrote:
> > > >
> > > >
> > > > >I guess I should read the whole question before I reply. I think I
> > > answered
> > > > >you question backwards.
> >
>
>

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

17. Re: how get "number" in disk text file read into integer variable?

hi Nick,

Not merely a chatbot,, "chat AI" is a major misnomer,, chatbots are not
intelligent. Gertie is occasionally on irc, occasionally reads web pages,
but mostly just talks to me here on the puter.

(bigger reply sent privately to avoid listserv flood)

Kat

----- Original Message -----
From: The Johnson Family <thedjs at INAME.COM>
To: <EUPHORIA at LISTSERV.MUOHIO.EDU>
Sent: Saturday, January 22, 2000 3:17 PM
Subject: Re: how get "number" in disk text file read into integer variable?


> What's this? A chat AI? Is it running on any servers?
>
> Nick
> ----- Original Message -----
> From: Kat <gertie at ZEBRA.NET>
> To: <EUPHORIA at LISTSERV.MUOHIO.EDU>
> Sent: Sunday, January 23, 2000 9:16 AM
> Subject: Re: how get "number" in disk text file read into integer
variable?
>
>
> > Nope, sorry Ad, Gertie is the potential Ai's name. I have teased that
> Gertie
> > would have her own email addy, so when i changed isps, i figured what
the
> > heck? She is using that name a bit prematurely for sure, since she is
> still
> > mostly mirc bot code, but with Eu plugged into mirc now, and slowly
moving
> > the Pascal code into Eu (and dealing with that paradigm shift), i hope
by
> > the end of the year that Gertie will be where i was in Pascal 5 yrs
ago,,
> > but with the future i can see using Eu. She already has better fault
> > tolerance in Eu, and a much larger database, and is significantly faster
> > than Pascal was (even tho the pascal ran in dos and used indexing into
the
> > database!). Mirc is being used as a very easily programmable gui and
> > human/internet interface, and generic mirc bot channel op stuff when on
> irc.
> > I can't tell you how nice it is to be able to kill Eu and still have the
> > gui/internet running with no errors, or kill mirc and still have the
core
> Eu
> > running.
> >  smile
> >
> > Kat
> > no, RDS and mIRC didn't pay me for this tongue

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

18. Re: how get "number" in disk text file read into integer variable?

PQ,

Thanks for the code, but I think I have solved my problem of getting an
integer return from a disk file read, by putting {} around the return from
disk read & then using "value" on it :

  temp = value({line[2]})  -- where the number is the second in position in
line
 anIntegerValue = temp[2]

I guess that will only work with single digits, but if I need to specify
more than 9 I think I can figure out how.

Dan
-----Original Message-----
From: PQ <quistnet at HOTMAIL.COM>
To: EUPHORIA at LISTSERV.MUOHIO.EDU <EUPHORIA at LISTSERV.MUOHIO.EDU>
Date: Saturday, January 22, 2000 11:07 AM
Subject: Re: how get "number" in disk text file read into integer variable?


>----- Original Message -----
>From: Dan B Moyer <DANMOYER at PRODIGY.NET>
>To: <EUPHORIA at LISTSERV.MUOHIO.EDU>
>Sent: Saturday, January 22, 2000 8:10 AM
>Subject: how get "number" in disk text file read into integer variable?
>
>
>> Ok gang, here's my problem now <sigh!>:
>>
>> How do I get a "number" I have saved into a (text) disk file back as an
>> integer?
>>
>> I'm trying to save a structured variable to a disk file & then be able to
>> read it from the file & put it back  into the structured variable.  I'm
>> using the
>> "newex06d.exw" database example by Ad Rienks from the Tutorial as a
>starting
>> point, & proceeding tiny steps at a time.
>>
>> My first step was to make a text file that would LOOK like the saved file
>> should look like, and then just READ that file into my structured
>variable.
>> Since part of my structured variable is of variable length, I put that
>> length into a disk file (using a text editor), so it can be read out &
>used
>> to count lines in the file to know what to put where in the structured
>> variable.
>>
>> So if my first variant length is "3", I put the number 3 into the disk
>> file on a line; but I can't seem to get that "3" back as an integer when
I
>> read it (I get 51, which is ASCII for 3).  I'm guessing this is
>> pathetically simple, but I can't get it.
>>
>> Dan Moyer
>>
>
>'3'- '0'=3
>51-48=3
>
>Or just use this:
>-- Begin MMATCH.E --
>global function multimatch(sequence str,sequence cstr)
>object loc,pl,temp1,temp2,temp3,lngt,t1,t2
> temp1=""
> temp2=cstr
> pl=0
> lngt=length(str)
> loc=match(str,temp2)
>  while loc do
>   temp3=loc+pl*lngt
>   temp1=temp1 & temp3
>   t1=temp2[1..loc-1]
>   t2=temp2[loc+lngt..length(temp2)]
>   temp2=t1 & t2
>   loc=match(str,temp2)
>   pl+=1
>  end while
>return temp1
>end function
>-- End MMATCH.E --
>----------------------------
>-- Begin SEQ2INT.E --
>include mmatch.e
>
>global function isAtom(sequence input)
>object returnment,tmp
>tmp=0
> for a=1 to length(input) do
>  if input[a]>='0' and input[a]<='9' then
>   tmp+=1
>  elsif input[a]='.' or input[a]='-' then
>   tmp+=1
>  end if
> end for
> if tmp=length(input) then
>  returnment=-1
> else
>  returnment=0
> end if
>return returnment
>end function
>
>-- Works till "9999999999", try "1.5" or "-1.5" it works --
>global function seq2int(sequence str)
>object result,tmp1,tmp2,divide
>divide=1
>result=0
>if length(str) !=0 then
> tmp1=find('.',str)
> tmp2=find('-',str)
> if tmp1 !=0 then
>  divide=power(10,length(str)-tmp1)
>  str=str[1..tmp1-1]&str[tmp1+1..length(str)]
> end if
> if tmp2 !=0 then
>  divide*=-1
>  str=str[1..tmp2-1]&str[tmp2+1..length(str)]
> end if
> result=str[length(str)]-48
> for a=1 to length(str)-1 do
>  result+=power(10,length(str)-a)*(str[a]-48)
> end for
> result/=divide
>end if
>return result
>end function
>-- End SEQ2INT.E --
>--------------------------
>
>I don't have to say 'I hope that this works', because it just does work...
>
>Bye,
>PQ
>QC

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

19. Re: how get "number" in disk text file read into integer variable?

Isn't IO in Euphoria simple and wonderful. Algorithms are beautiful in
Euphoria, but this would be about half the size and a whole lot more
intuitive in COBOL. Just one of those advantages of not knowing what
anything actually looks like. This doesn't mean that I am against
Euphoria's variables for internal use, but this example graphically shows how
much fun it is to just read in what was written out, much less read external
data of known fixed or variable format in. We need the ability to define
arbitrary external data, even if it is only for IO routines. This really begs
for
structures and/or named offsets.

Everett L.(Rett) Williams
rett at gvtc.com


 PQ  wrote:
>
>Or just use this:
>-- Begin MMATCH.E --
>global function multimatch(sequence str,sequence cstr)
>object loc,pl,temp1,temp2,temp3,lngt,t1,t2
> temp1=""
> temp2=cstr
> pl=0
> lngt=length(str)
> loc=match(str,temp2)
>  while loc do
>   temp3=loc+pl*lngt
>   temp1=temp1 & temp3
>   t1=temp2[1..loc-1]
>   t2=temp2[loc+lngt..length(temp2)]
>   temp2=t1 & t2
>   loc=match(str,temp2)
>   pl+=1
>  end while
>return temp1
>end function
>-- End MMATCH.E --
>----------------------------
>-- Begin SEQ2INT.E --
>include mmatch.e
>
>global function isAtom(sequence input)
>object returnment,tmp
>tmp=0
> for a=1 to length(input) do
>  if input[a]>='0' and input[a]<='9' then
>   tmp+=1
>  elsif input[a]='.' or input[a]='-' then
>   tmp+=1
>  end if
> end for
> if tmp=length(input) then
>  returnment=-1
> else
>  returnment=0
> end if
>return returnment
>end function
>
>-- Works till "9999999999", try "1.5" or "-1.5" it works --
>global function seq2int(sequence str)
>object result,tmp1,tmp2,divide
>divide=1
>result=0
>if length(str) !=0 then
> tmp1=find('.',str)
> tmp2=find('-',str)
> if tmp1 !=0 then
>  divide=power(10,length(str)-tmp1)
>  str=str[1..tmp1-1]&str[tmp1+1..length(str)]
> end if
> if tmp2 !=0 then
>  divide*=-1
>  str=str[1..tmp2-1]&str[tmp2+1..length(str)]
> end if
> result=str[length(str)]-48
> for a=1 to length(str)-1 do
>  result+=power(10,length(str)-a)*(str[a]-48)
> end for
> result/=divide
>end if
>return result
>end function
>-- End SEQ2INT.E --
>--------------------------
>
>I don't have to say 'I hope that this works', because it just does work...
>
>Bye,
>PQ
>QC

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

20. Re: how get "number" in disk text file read into integer variable?

> Isn't IO in Euphoria simple and wonderful. Algorithms are beautiful in
> Euphoria, but this would be about half the size and a whole lot more
> intuitive in COBOL. Just one of those advantages of not knowing what
> anything actually looks like. This doesn't mean that I am against
> Euphoria's variables for internal use, but this example graphically shows
how
> much fun it is to just read in what was written out, much less read
external
> data of known fixed or variable format in. We need the ability to define
> arbitrary external data, even if it is only for IO routines. This really
begs for
> structures and/or named offsets.

It seems to me that the main problem is in the various formats data can be
read/written in. The simplest way is to write data cout using print, then
read using get. (eg. 3 would be {3}) This can be used for any format data,
but is not very space efficient.
Secondly, a number could be written in ASCII format (eg. 3 would be 3), then
read in as a string sequence, then converted to an atom with value()
Thirdly, and most space efficiently, a number could be written in binary
(eg. 3 would be the ascii character for 3). This limits numbers to 0-255,
but could be extended to any number with addition of extra bytes. This
number could be read using getc()
So the real problem is depends on how you write your data. Reading in and
then getting the character you want in euphoria is much like asking for
asc(char) in basic - you will get the character code for the character in
the string instead.

I'm probably confusing everyone, so I'll shut up.

Nick

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

21. Re: how get "number" in disk text file read into integer variable?

Nick wrote:

>It seems to me that the main problem is in the various formats data can be
>read/written in.

I'm not sure, but I think you have hit the nail on the head.

><snip> a number could be written in ASCII format (eg. 3 would be 3), then
>read in as a string sequence, then converted to an atom with value()

 I think I am using that method now, because I wrote a test text file using
text editor in order to test reading the file data into a data structure &
Brian suggested using "value" to get the number, & then I discovered that I
had to put {} around the variable that data was read into to make it work.

>The simplest way is to write data cout using print, then
>read using get. (eg. 3 would be {3}) This can be used for any format data,
>but is not very space efficient.

This is what I eventually suspected might be the "best" way, but wasn't
really sure. (I am pretty sure you meant "data out"?)

>Thirdly, and most space efficiently, a number could be written in binary
>(eg. 3 would be the ascii character for 3). This limits numbers to 0-255,
>but could be extended to any number with addition of extra bytes. This
>number could be read using getc()

This does confuse me: wouldn't binary 3 be 11, not ascii char for 3?

>So the real problem is depends on how you write your data. Reading in and
>then getting the character you want in euphoria is much like asking for
>asc(char) in basic - you will get the character code for the character in
>the string instead.

Yes!?

>
>I'm probably confusing everyone, so I'll shut up.

No, no, *sometimes* enough confusion will resolve to illumination, if I'm
lucky or persistent.


Dan

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

22. Re: how get "number" in disk text file read into integer variable?

Hi Dan,

I use this code to write and read integers to and from a disk file. fn is a file
handle.

global procedure PutDWord(integer fn, integer dw)
--  Writes a double word (4 bytes) to an open file.
    puts(fn,int_to_bytes(dw))
end procedure

global function GetDWord(integer fn)
-- Reads an 32-bits integer from disk.
-- Speeding up thanks to Lucius L. Hilley III.
    sequence w

    w = repeat(0,4)
    for i = 1 to 4 do
 w[i] = getc(fn)
    end for
    return bytes_to_int(w)
end function

-- end code

Dan B Moyer wrote:

> Ok gang, here's my problem now <sigh!>:
>
> How do I get a "number" I have saved into a (text) disk file back as an
> integer?
>
> I'm trying to save a structured variable to a disk file & then be able to
> read it from the file & put it back  into the structured variable.  I'm
> using the
> "newex06d.exw" database example by Ad Rienks from the Tutorial as a starting
> point, & proceeding tiny steps at a time.
>
> My first step was to make a text file that would LOOK like the saved file
> should look like, and then just READ that file into my structured variable.
> Since part of my structured variable is of variable length, I put that
> length into a disk file (using a text editor), so it can be read out & used
> to count lines in the file to know what to put where in the structured
> variable.
>
> So if my first variant length is "3", I put the number 3 into the disk
> file on a line; but I can't seem to get that "3" back as an integer when I
> read it (I get 51, which is ASCII for 3).  I'm guessing this is
> pathetically simple, but I can't get it.
>
> Dan Moyer

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

23. Re: how get "number" in disk text file read into integer variable?

----- Original Message -----
From: Dan B Moyer <DANMOYER at PRODIGY.NET>
To: <EUPHORIA at LISTSERV.MUOHIO.EDU>
Sent: Saturday, January 22, 2000 2:49 AM
Subject: Re: how get "number" in disk text file read into integer variable?


There have been lots of answers on this, but I wondered if it would be
possible
to write a generic routine that would read and recognize integers, atoms,
and
strings (either quoted or unquoted) from standard text files without
problems.
Yes - with one exception. An unquoted string cannot be followed by a number
on the same line. All other combinations of numbers, comma separators,
oddball separator characters, etc. work fine.

Here's my test file:
1 3    5.01,7.0120:9,Hello
"PI equals ",      3.14159
1e4
5
This is the end of the file

Now for the routine and a test stub:
---------------
-- I/O tests --
---------------
include file.e
include get.e
constant EOF = -1, NULL = 0

function read(integer fn)
object input
 input = get(fn)
 if input[1] = -1 then return EOF
 elsif sequence(input[2]) then return({input[2]}) -- quoted string
 elsif input[1] = 0 then return(input[2]) -- number
 elsif input[1] = 1 then  -- unquoted string
       input = seek(fn,where(fn)-1) -- rewind one byte
       input = gets(fn)
       if equal({13,10},input) then -- ignore cr/lf pairs
          return NULL
       else
          return {input[1..length(input)-1]} -- strip cr before returning
       end if
 end if
end function


integer fn
object x

-- Test stub
   fn = open("text.txt","rb")
   while 1 do
     x = read(fn)
     if equal(x,EOF) then exit
     elsif equal(x,NULL) then -- do nothing
     elsif sequence(x) then printf(1,"String: %s\n",x)
     elsif integer(x) then printf(1,"Integer: %d\n",x)
     else printf(1,"Atom: %8.2f\n",x)
     end if
   end while
   close(fn)

Irv

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

24. Re: how get "number" in disk text file read into integer variable?

I'm sorry, but I don't quite get the jist of your message- are you asking
for a solution to a problem, a new routine to read and distinguish different
data types from an input file, or just posting a copy of the working
routine?

Nick

----- Original Message -----
From: Irv Mullins <irv at ELLIJAY.COM>
To: <EUPHORIA at LISTSERV.MUOHIO.EDU>
Sent: Thursday, January 27, 2000 4:58 AM
Subject: Re: how get "number" in disk text file read into integer variable?


> ----- Original Message -----
> From: Dan B Moyer <DANMOYER at PRODIGY.NET>
> To: <EUPHORIA at LISTSERV.MUOHIO.EDU>
> Sent: Saturday, January 22, 2000 2:49 AM
> Subject: Re: how get "number" in disk text file read into integer
variable?
>
>
> There have been lots of answers on this, but I wondered if it would be
> possible
> to write a generic routine that would read and recognize integers, atoms,
> and
> strings (either quoted or unquoted) from standard text files without
> problems.
> Yes - with one exception. An unquoted string cannot be followed by a
number
> on the same line. All other combinations of numbers, comma separators,
> oddball separator characters, etc. work fine.
>
> Here's my test file:
> 1 3    5.01,7.0120:9,Hello
> "PI equals ",      3.14159
> 1e4
> 5
> This is the end of the file
>
> Now for the routine and a test stub:
> ---------------
> -- I/O tests --
> ---------------
> include file.e
> include get.e
> constant EOF = -1, NULL = 0
>
> function read(integer fn)
> object input
>  input = get(fn)
>  if input[1] = -1 then return EOF
>  elsif sequence(input[2]) then return({input[2]}) -- quoted string
>  elsif input[1] = 0 then return(input[2]) -- number
>  elsif input[1] = 1 then  -- unquoted string
>        input = seek(fn,where(fn)-1) -- rewind one byte
>        input = gets(fn)
>        if equal({13,10},input) then -- ignore cr/lf pairs
>           return NULL
>        else
>           return {input[1..length(input)-1]} -- strip cr before returning
>        end if
>  end if
> end function
>
>
> integer fn
> object x
>
> -- Test stub
>    fn = open("text.txt","rb")
>    while 1 do
>      x = read(fn)
>      if equal(x,EOF) then exit
>      elsif equal(x,NULL) then -- do nothing
>      elsif sequence(x) then printf(1,"String: %s\n",x)
>      elsif integer(x) then printf(1,"Integer: %d\n",x)
>      else printf(1,"Atom: %8.2f\n",x)
>      end if
>    end while
>    close(fn)
>
> Irv
>
>

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

25. Re: how get "number" in disk text file read into integer variable?

A belated thanks to M.Kollenaar for code to write & read integers from a
disk file; turns out I did need it after all, for a different part of my
structured variable than what I was originally asking about.

Dan Moyer


-----Original Message-----
From: M.Kollenaar <M.Kollenaar at SLO.NL>
To: EUPHORIA at LISTSERV.MUOHIO.EDU <EUPHORIA at LISTSERV.MUOHIO.EDU>
Date: Sunday, January 23, 2000 1:51 PM
Subject: Re: how get "number" in disk text file read into integer variable?


>Hi Dan,
>
>I use this code to write and read integers to and from a disk file. fn is a
file
>handle.
>
>global procedure PutDWord(integer fn, integer dw)
>--  Writes a double word (4 bytes) to an open file.
>    puts(fn,int_to_bytes(dw))
>end procedure
>
>global function GetDWord(integer fn)
>-- Reads an 32-bits integer from disk.
>-- Speeding up thanks to Lucius L. Hilley III.
>    sequence w
>
>    w = repeat(0,4)
>    for i = 1 to 4 do
> w[i] = getc(fn)
>    end for
>    return bytes_to_int(w)
>end function
>
>-- end code
>
>Dan B Moyer wrote:
>
>> Ok gang, here's my problem now <sigh!>:
>>
>> How do I get a "number" I have saved into a (text) disk file back as an
>> integer?
>>
>> I'm trying to save a structured variable to a disk file & then be able to
>> read it from the file & put it back  into the structured variable.  I'm
>> using the
>> "newex06d.exw" database example by Ad Rienks from the Tutorial as a
starting
>> point, & proceeding tiny steps at a time.
>>
>> My first step was to make a text file that would LOOK like the saved file
>> should look like, and then just READ that file into my structured
variable.
>> Since part of my structured variable is of variable length, I put that
>> length into a disk file (using a text editor), so it can be read out &
used
>> to count lines in the file to know what to put where in the structured
>> variable.
>>
>> So if my first variant length is "3", I put the number 3 into the disk
>> file on a line; but I can't seem to get that "3" back as an integer when
I
>> read it (I get 51, which is ASCII for 3).  I'm guessing this is
>> pathetically simple, but I can't get it.
>>
>> Dan Moyer

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

26. Re: how get "number" in disk text file read into integer variable?

----- Original Message -----
From: Dan B Moyer <DANMOYER at PRODIGY.NET>
To: <EUPHORIA at LISTSERV.MUOHIO.EDU>
Sent: Monday, January 31, 2000 2:02 AM
Subject: Re: how get "number" in disk text file read into integer variable?


> A belated thanks to M.Kollenaar for code to write & read integers from a
> disk file; turns out I did need it after all, for a different part of my
> structured variable than what I was originally asking about.

Dan:

Could you share with us one line from the file you are reading? The reason
I ask is that I have not found an instance where Euphoria couldn't
read human-readable data from a file using the standard input commands.
(integers and reals stored by VB, C and Pascal are another story)

Regards,
Irv

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

Search



Quick Links

User menu

Not signed in.

Misc Menu