1. I need help! (again :>)
- Posted by Joseph Martin <jam at MAILHUB.EXIS.NET>
Jun 02, 1997
-
Last edited Jun 03, 1997
I'm writing a program to read in a file and change certain pieces of
text to something else. For instance in the file I would change all
instances of '1' to '2'. However when I try to "find" the text I want
to change in my sample file it returns 0. Can anyone help?
<CODE>
include get.e
include file.e
without warning
with trace
sequence cmd, file, x
atom fn, n, m
object tmp, line
x = {}
cmd = command_line()
if length(cmd) < 3 then
puts(1, "Wrong options. Correct format is:\n")
puts(1, "filter <input> <output>\n")
puts(1, "<input> - file to filter\n")
puts(1, "<output> - name of new file\n")
abort(1)
end if
tmp = dir(cmd[3])
if atom(tmp) then
puts(1, "File not found!\n")
abort(1)
end if
fn = open(cmd[3], "r")
file = {}
while 1 do
line = gets(fn)
if atom(line) then
exit
end if
file = append(file, line)
end while
close(fn)
trace(1)
? file[1]
for i = 1 to length(file) do
n = find("1", file[i])
if n != -1 then
m = i - 1
x = file[i][1..m]
x = x & "2"
m = i + 1
x = x & file[i][m..length(file[i])]
file[i] = x
end if
end for
fn = open(cmd[4], "w")
for i = 1 to length(file) do
printf(fn, "%s", file[i])
end for
close(fn)
trace(0)
</CODE>
~~>Joseph Martin
~~>E-mail: joe at cyber-wizard.com
~~>URL: http://users.exis.net/~jam/
2. Re: I need help! (again :>)
>
> The problem is that find returns the postion at which
> the find was made.
> If find didn't find anything then it found it at 0.
> or nowhere.
>
> find returns WHERE or 0
> not -1
>
> find never returns -1
Granted. I forgot that.
> >for i = 1 to length(file) do
> > n = find("1", file[i])
> > if n != -1 then
>
> should be changed to
>
> for i = 1 to length(file) do
> n = find("1", file[i])
> if n then
> OR
>
> for i = 1 to length(file) do
> n = find("1", file[i])
> if n > 0 then
> OR
>
> for i = 1 to length(file) do
> n = find("1", file[i])
> if n != 0 then
>
My real problem is that the keyword is in the file and "find" doesn't find it!
3. I need help! (again :>)
Joseph Martin's program contains:
> n =3D find("1", file[i])
file[i] is a string of characters.
To locate a string within another string use match():
n =3D match("1", file[i])
You could also try to match a longer string:
n =3D match("Joseph", file[i])
Or, to locate a single character within a string use find():
n =3D find('1', file[i])
Note the difference between '1' and "1".
'1' is an atom. "1" is a length-1 sequence. They are not the same.
Your find() always fails because no element of sequence file[i]
is a length-1 sequence "1". The elements of file[i] are all =
single-character atoms.
=
Regards,
Rob Craig
Rapid Deployment Software
4. Re: I need help! (again :>)
Yeah what Robert said.
match for double quotes. (strings)
find use single quites.
He beat me to that one.
That kinda slipped by me when I skimmed your
message.
--Lucius Lamar Hilley III
-- E-mail at luciuslhilleyiii at juno.com
-- I support transferring of files less than 60K.
-- I can Decode both UU and Base64 format.
5. Re: I need help! (again :>)
Thanks for all the help. I actually finished the program yesterday.
After I clean up the code a bit I'll post it to the ftp site and sent
it to RDS.
~~>Joseph Martin
~~>E-mail: joe at cyber-wizard.com
~~>URL: http://users.exis.net/~jam/
6. Re: I need help! (again :>)
- Posted by Michael Bolin <michaeltom at GEOCITIES.COM>
Jun 02, 1997
-
Last edited Jun 03, 1997
> I'm writing a program to read in a file and change certain pieces of
> text to something else. For instance in the file I would change all
> instances of '1' to '2'. However when I try to "find" the text I want
> to change in my sample file it returns 0. Can anyone help?
There are several bugs in your main loop. Replace it with this one,
and it should work:
for i = 1 to length(file) do
n = find('1', file[i])
if n != 0 then
x = file[i][1..n-1] & '2' & file[i][n+1..length(file[i])]
file[i] = x
end if
end for
You need to search for an atom ( '1' ), not a sequence ( "1" ).
Also, if the value is not found, find() will return 0, not -1.
Lastly, your replace routine is written incorrectly. The simple loop
above will work instead.
Regards,
Michael Bolin