1. Text lines into an array of unknown size
- Posted by kjactive at adslhome.dk
Aug 08, 2003
This is a multi-part message in MIME format.
------=_NextPart_000_0011_01C35DD5.0F2CD740
charset="iso-8859-1"
as new commer from rexx to Euphoria I see many nice and semilare options =
and a richness of WinOS options but I have some
basic newcommer problems on 'sequence a array'=20
I want to open a file of text of unknown size and put all lines into an =
array where every line can be manipulated but I cannot get it right...
integer file
object line
constant ERROR =3D 2
integer c
c =3D 0
sequence tx
file =3D open("c:/rexx/rxcalibur.txt", "r")
if file =3D -1 then
puts(ERROR, "couldn't open text file\n")
else
while 1 do
line =3D gets(file)
if atom(line) then
exit
end if
c =3D c + 1
tx[c] =3D line
end while
end if
close(file)=20
printf(1,"available lines:%d\n", {c}) -- correct
printf(1,"%s\n",tx[2]) -- ERROR=20
Where do I get it wrong!!! Probably a newcommers init problem but I =
would like some explane...
K=E5re Johansson
------=_NextPart_000_0011_01C35DD5.0F2CD740
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.2800.1170" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3DArial size=3D2>
<DIV><FONT face=3DArial size=3D2>as new commer from rexx to Euphoria I =
see many=20
nice and semilare options and a richness of WinOS options but I =
have=20
some</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>basic newcommer problems on 'sequence a =
array'=20
</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3DArial size=3D2>I want to open a file of text of =
unknown size and=20
put all lines into an array where every line can be=20
manipulated but I cannot get it right...</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3DArial size=3D2>integer file<BR>object line<BR>constant =
ERROR =3D=20
2<BR>integer c<BR>c =3D 0</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>sequence tx</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>file =3D open("c:/rexx/rxcalibur.txt", =
"r")<BR>if=20
file =3D -1 then<BR> puts(ERROR, "couldn't open text=20
file\n")<BR>else<BR> while 1 do<BR> line =3D=20
gets(file)<BR> if atom(line)=20
then<BR> exit<BR> end if<BR> c =3D c +=20
1</FONT></DIV>
<DIV><FONT face=3DArial size=3D2> tx[c] =3D line<BR> end =
while<BR>end=20
if<BR>close(file) <BR>printf(1,"available lines:%d\n", {c}) --=20
correct<BR>printf(1,"%s\n",tx[2]) -- ERROR </FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>Where do I get it wrong!!! =
</FONT>Probably a=20
newcommers init problem but I would like some explane...</DIV>
<DIV> </DIV>
------=_NextPart_000_0011_01C35DD5.0F2CD740--
2. Re: Text lines into an array of unknown size
Hi K=E5re, you wrote:
> as new commer from rexx to Euphoria
Welcome!
> I see many nice and semilare options and a richness of WinOS options
> but I have some basic newcommer problems on 'sequence a array'
>
> I want to open a file of text of unknown size and put all lines into
> an array where every line can be manipulated but I cannot get it right...=
There are no real "arrays" in Euphoria, but you can think of a sequence
as "very flexible array-like" data type.
Since the numbers of lines in a text file normally is not know before
reading the file, a sequence is much easier to use for reading a text
file line by line, than an array (no dim/redim required).
[code snipped]
> Where do I get it wrong!!! Probably a newcommers init problem but I
> would like some explane...
This is how I would do it:
constant ERROR =3D 2
integer count, file
sequence tx
object line
file =3D open("c:/rexx/rxcalibur.txt", "r")
if file =3D -1 then
puts(ERROR, "couldn't open text file\n")
abort(1) -- end of program, exit code 1
end if
count =3D 0
tx =3D {}
while 1 do
line =3D gets(file)
if atom(line) then
exit
end if
count +=3D 1
tx =3D append(tx, line)
end while
close(file)
printf(1, "available lines: %d\n\n", {count})
for i =3D 1 to count do
puts(1, tx[i])
end for
Best regards,
Juergen
--=20
/"\ ASCII ribbon campain |
\ / against HTML in | This message has been ROT-13 encrypted
X e-mail and news, | twice for higher security.
/ \ and unneeded MIME |
3. Re: Text lines into an array of unknown size
On Fri, 8 Aug 2003 17:46:56 +0200, kjactive at adslhome.dk wrote:
Hi there.
Matt's quite right, but missed two other errors:
>file =3D open("c:/rexx/rxcalibur.txt", "r")
should be
file =3D open("c:\\rexx\\rxcalibur.txt", "r")
and
>printf(1,"%s\n",tx[2])=20
should be
printf(1,"%s\n",{tx[2]})=20
otherwise it will only print the first character of the line.
Pete
4. Re: Text lines into an array of unknown size
On Fri, 08 Aug 2003 17:38:38 +0100, Pete Lomax
<petelomax at blueyonder.co.uk> wrote:
Oops:
>Matt's quite right, but missed two other errors:
>>file =3D open("c:/rexx/rxcalibur.txt", "r")
>
>should be
>
>file =3D open("c:\\rexx\\rxcalibur.txt", "r")
>
scratch that, I was thinking of something else. Both work just fine in
Euphoria.
Sorry,
Pete
5. Re: Text lines into an array of unknown size
*One* special character in the mail, and Topica mangled almost the
whole mail ... Grrrrr!
Here's the code again (the variable 'count' wasn't necessary anyway):
constant ERROR = 2
integer file
sequence tx
object line
file = open("c:/rexx/rxcalibur.txt", "r")
if file = -1 then
puts(ERROR, "couldn't open text file\n")
abort(1) -- end of program, exit code 1
end if
tx = {}
while 1 do
line = gets(file)
if atom(line) then
exit
end if
tx = append(tx, line)
end while
close(file)
printf(1, "available lines: %d\n\n", {length(tx)})
for i = 1 to length(tx) do
puts(1, tx[i])
end for
Best regards,
Juergen
--
/"\ ASCII ribbon campain |
\ / against HTML in | This message has been ROT-13 encrypted
X e-mail and news, | twice for higher security.
/ \ and unneeded MIME |
6. Re: Text lines into an array of unknown size
- Posted by kjactive at adslhome.dk
Aug 08, 2003
----- Original Message -----
From: "Pete Lomax" <petelomax at blueyonder.co.uk>
To: "EUforum" <EUforum at topica.com>
Subject: Re: Text lines into an array of unknown size
>
>
> On Fri, 8 Aug 2003 17:46:56 +0200, kjactive at adslhome.dk wrote:
>
> Hi there.
> Matt's quite right, but missed two other errors:
> >file =3D open("c:/rexx/rxcalibur.txt", "r")
>
> should be
>
> file =3D open("c:\\rexx\\rxcalibur.txt", "r")
>
> and
> >printf(1,"%s\n",tx[2])
>
> should be
>
> printf(1,"%s\n",{tx[2]})
>
> otherwise it will only print the first character of the line.
>
> Pete
>
RE| thanks just what I needed
K=E5re Johansson