1. strange error
I've come across a small error in a program I am writing. I am trying
to scan an incomming buffer for known characters, and came up with the
following code:
for iBufPos = 1 to length(iBuffer)
[0;30m FoundAt = find(iBuffer[iBufPos], Chars)
[0;30m if FoundAt then
DoSomething
end if
end for
iBufPos and FoundAt are atoms, and iBuffer and Chars are both sequences.
My reasoning follows:
1. look at each character in iBuffer, and if found in Chars go do
something.
However, when I try to run the program I get the following error:
Attempt to redifine iBufPos
FoundAt = find(iBuffer[iBufPos], Chars)
^
I do not understand how I could be redefining iBufPos. I am using it as
a for loop counter, stepping through iBuffer. I know that I cannot
asign a new value to iBufPos (and by looking at the code, I don't see
that happening). I thought find used the first arguement as the search
criteria, and the second as the place to search in. I do not think that
using the value in iBufPos as a pointer to a single character inside
iBuffer should be causing a redefinition of iBufPos.
Any help would be greatly appreciated.
Thanks,
James Powell
2. Re: strange error
At 03:26 AM 6/28/97 -0500, you wrote:
> for iBufPos = 1 to length(iBuffer)
> .......
> end for
>iBufPos and FoundAt are atoms
>Attempt to redifine iBufPos
> FoundAt = find(iBuffer[iBufPos], Chars)
> ^
The variable used to loop thru the 'for' statement
cannot not be previously defined.
For example:
integer a
for a = 1 to 10 do
-- code
end for
generates the same error message as you are receiving
--dae at paclink.com--
3. Re: strange error
EU>> for iBufPos = 1 to length(iBuffer)
EU>> .......
EU>> end for
EU>>iBufPos and FoundAt are atoms
EU>>Attempt to redifine iBufPos
EU>> FoundAt = find(iBuffer[iBufPos], Chars)
EU>> ^
EU>The variable used to loop thru the 'for' statement
EU> cannot not be previously defined.
EU> For example:
EU>integer a
EU>for a = 1 to 10 do
EU> -- code
EU>end for
EU> generates the same error message as you are receiving
EU>--dae at paclink.com--
Thanks, I'll try to remember that! (BTW, I saw the answer in the manual
about 5 mins after I sent my help request... just one more reason to
RTFM I guess
)
James P.
4. Re: strange error
On Sat, 28 Jun 1997 03:26:56 -0500 James Powell <wizard at DJO.COM> writes
about:
Subject: strange error
>I've come across a small error in a program I am writing. I am trying
>to scan an incomming buffer for known characters, and came up with the
>following code:
>Attempt to redifine iBufPos
> FoundAt = find(iBuffer[iBufPos], Chars)
> ^
>From the Library.Doc about find(x,s)
Description: Find x as an element of s. If successful, return the index
of the first element of s that matches. If unsuccessful
return 0.
You could also say Find x in s where x is a member of the sequence s.
Your code is trying to find s in x.
Rewrite as FoundAT=find(Chars,iBuffer).
FoundAt will contain the position of Chars if found and 0 if not. If
iBuffer is a sequence of sequences then you will have to step though it
recursively to locate the Chars variable.
Here is a short example of how Find works. It is not written in tight
code the hope of showing how find(x,s) works. I am just a beginner with
Euphoria so I hope I this is clear enough.
Larry D. Poos
-[USMC (Retar{bks}{bks}ired) Havelock, NC]-
- Programming and System Consultant, LTAD Enterprises -
e-mail: ldpoos at juno.com
Fido: 1:3629/101.6
---------------- Begin plain text code segment ------------
with trace
-- initialize testing variables
sequence iBuffer
object FoundAt, SearchFor,tempbuf
SearchFor=3 -- you can change this number to any number 10 or less
iBuffer={}
-- build some data sequences
for x=1 to 10 do
tempbuf={}
iBuffer=append(iBuffer,{x})
for y=7 to 10 do
tempbuf=append(tempbuf,rand(y))
end for
iBuffer[x]=append(iBuffer[x],tempbuf)
end for
-- show what we are searching
?iBuffer
puts(1,"\n")
-- the meat of the search
trace(1) -- watch how it works
for x=1 to length(iBuffer) do -- loop through each segment
for y=1 to length(iBuffer[x]) do -- loop through the subsegments
tempbuf=iBuffer[x][y] -- temp storage for what we are going to
test
if atom(tempbuf) then -- ensure test item is a sequence
tempbuf={tempbuf}
end if
FoundAt=find(SearchFor,tempbuf) -- look for test item
if FoundAt then -- display search results
printf(1,"%g Found in iBuffer[%d][%d] at position %d\n",
{SearchFor,x,y,FoundAt})
else
printf(1,"Item not Found in iBuffer[%d][%d]\n",{x,y})
end if
end for
end for
---------------- End plain text code segment ------------
5. Re: strange error
> I've come across a small error in a program I am writing. I am trying
> to scan an incomming buffer for known characters, and came up with the
> following code:
> for iBufPos = 1 to length(iBuffer)
> [0;30m FoundAt = find(iBuffer[iBufPos], Chars)
> [0;30m if FoundAt then
> DoSomething
> end if
> end for
> iBufPos and FoundAt are atoms, and iBuffer and Chars are both sequences.
> My reasoning follows:
> 1. look at each character in iBuffer, and if found in Chars go do
> something.
> However, when I try to run the program I get the following error:
> Attempt to redifine iBufPos
> FoundAt = find(iBuffer[iBufPos], Chars)
> ^
> I do not understand how I could be redefining iBufPos. I am using it as
> a for loop counter, stepping through iBuffer. I know that I cannot
> asign a new value to iBufPos (and by looking at the code, I don't see
> that happening). I thought find used the first arguement as the search
> criteria, and the second as the place to search in. I do not think that
> using the value in iBufPos as a pointer to a single character inside
> iBuffer should be causing a redefinition of iBufPos.
> Any help would be greatly appreciated.
Make sure that you have not previously defined iBufPos with a command
like :
atom iBufPos
Using a for-loop automatically defines a variable for you.
Other than that, your code is correct except that the line:
> for iBufPos = 1 to length(iBuffer)
should read:
for iBufPos = 1 to length(iBuffer) do
For-loops and while-loops need the word "do" before the actual code
inside the loop.
Regards,
Michael Bolin