Help with sting compare?

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Help with sting compare?

Ron Hudson-2
I can do this from interactive lua and it seems to work, but when I use it in
a lua program it does not work..

I am reading through a file that has variable length records, records are separated
with a line containing only the "&". The last line in the file has "&&".

repeat
    aline = read.io()
    if aline == "&" then
      count = count + 1
    end
until aline == "&&"

Neither "&" or "&&" are ever detected.
I have checked the file... & and && are the only thing in the line.
I also have ">" .. aline .. "<" printed and see >&<

I don't get it... it should be straight forward.

Reply | Threaded
Open this post in threaded view
|

Re: Help with sting compare?

Philippe Lhoste
Ron Hudson wrote:
I am reading through a file that has variable length records, records are separated
with a line containing only the "&". The last line in the file has "&&".

repeat
     aline = read.io()
     if aline == "&" then
       count = count + 1
     end
until aline == "&&"

Neither "&" or "&&" are ever detected.
I have checked the file... & and && are the only thing in the line.
I also have ">" .. aline .. "<" printed and see >&<

I don't know what your read.io function is, but I hope it really returns only one line per call... Now, perhaps you should also check if the line includes the end-of-line character(s) (\n, \r or \r\n). If that's the case, trim them off with a gsub.

--
Philippe Lhoste
--  (near) Paris -- France
--  http://Phi.Lho.free.fr
--  --  --  --  --  --  --  --  --  --  --  --  --  --

Reply | Threaded
Open this post in threaded view
|

Re: Help with sting compare?

Adrian Sietsma
In reply to this post by Ron Hudson-2
Ron Hudson wrote:
I can do this from interactive lua and it seems to work, but when I use it in
a lua program it does not work..

I am reading through a file that has variable length records, records are separated
with a line containing only the "&". The last line in the file has "&&".

repeat
    aline = read.io()
    if aline == "&" then
      count = count + 1
    end
until aline == "&&"

Neither "&" or "&&" are ever detected.
I have checked the file... & and && are the only thing in the line.
I also have ">" .. aline .. "<" printed and see >&<

I don't get it... it should be straight forward.

try declaring aline before the repeat...until block.

i had a similar problem which this approach fixed - i surmised that it was a variable scoping issue.



Adrian

Reply | Threaded
Open this post in threaded view
|

Re: Help with sting compare?

Ron Hudson-2
In reply to this post by Philippe Lhoste


Philippe,

read.io()  gets one line from the current file, or standard input.

I am now using string.sub(aline,1,1) and string.sub(aline,1,2) to be sure I only check the first and second characters of the string aline... still it does not detect "&" or "&&"


Adrian,

I have put "local aline" at the beginning of the outermost loop, that did not fix it either.

I am printing out the substring of aline...

print(">" .. string.sub(aline,1,1) .. "<")

and I get:

>&<

So aline has the character I am looking for,  is == valid with strings?

I have also tried testing with '&' and enclosing the test in parens like C

if ( string.sub(aline,1,1) == "&") then....


I am using Lua 5.0.2....

Thanks for your help so far...

Ron.





Reply | Threaded
Open this post in threaded view
|

Re: Help with sting compare?

Rici Lake-2

On 18-Aug-05, at 11:18 PM, Ron Hudson wrote:



Philippe,

read.io()  gets one line from the current file, or standard input.


Actually, that's io.read() :) I assume you actually got it right in your program.

The following little Lua program works (io.lines is a much easier way of iterating lines of a file, but it's not actually different from the repeat loop):

----> transcript:

rlake@freeb:~$ cat hudson.test
foo
bar
&
glitch
&
comma
&&
ignored
&
rlake@freeb:~$ cat hudson.lua
count = 0
for aline in io.lines() do
  if aline == "&" then count = count + 1
  elseif aline == "&&" then break
  end
end
print(count)

rlake@freeb:~$ lua hudson.lua < hudson.test
2

<------ end of transcript

I believe that's the expected result.

Can't help you more without knowing a lot more details.


Reply | Threaded
Open this post in threaded view
|

Re: Help with sting compare?

Ron Hudson-2

On Aug 18, 2005, at 9:42 PM, Rici Lake wrote:


count = 0
for aline in io.lines() do
  if aline == "&" then count = count + 1
  elseif aline == "&&" then break
  end
end
print(count)

I tried replacing my io.read() with your io.lines() construct, but because the if statements are still not working for me mine is still not detecting the "&"

Here is my program as it currently stands:

#! /usr/local/bin/lua

-- Read fortunes in
do
    local aline

    jar = {}
    pointer = 0
    cookie = 1
    for aline in io.lines() do
        jar[cookie .. "+" ..  pointer] = aline
        pointer = pointer + 1
print(">" .. string.sub(aline,1,2) .. "< " .. cookie .. " " .. pointer)

        if ( string.sub(aline,1,2) == "&&")  then
            break
        end

        if ( string.sub(aline,1,1) == "&") then
            pointer = 0
            cookie = cookie + 1
        end
    end

    print(pointer .. " " .. cookie)
end

This incorporates your change, but cookie still increments for me, and pointer is never re-zeroed, In your example none of your if statements had a finishing end,
am I doing that wrong??

do I need to do

if (condition) then do
    statement
    statement
end

I am trying that with the code but it is confusing me....




Reply | Threaded
Open this post in threaded view
|

Re: Help with sting compare? Sorry, nevermind...

Ron Hudson-2
AARRRGGHHH !!!

Ok so I am blind!!!! ... my datafile has % and %% sentinels not & and &&

no wonder the if statement was not finding them!!.

Sorry to waste y'alls time.

Ron.