1. Testing for a empty string

I finally got my function to run with no errors (yeah!) but I think I'm still
doing something wrong.  My code:

for i = 1 to Lines do           
        -- Begin a loop that reads in each line from the text file
        --then, if strings[i] does not equal ; (a comment line) or
        --"" (a blank line), it appends the data into the sequence
        --DataRead
        strings[i] = gets(FileName)
        
        if strings[i][1] != {";"} or {" "} then
            DataRead = append ({}, strings[i])
        end if
    end for 

I want to test the 1st character of the line I received from the gets to see
if it's a semicolon or empty, if it is I don't want that line, if not then
I want to append it to DataRead.  Is the above correct?

Thanks!
Rich

new topic     » topic index » view message » categorize

2. Re: Testing for a empty string

Rich Klender wrote:
> 
>         strings[i] = gets(FileName)
>         
>         if strings[i][1] != {";"} or {" "} then
>             DataRead = append ({}, strings[i])
>         end if
> 
> I want to test the 1st character of the line I received from the gets to see
> if it's a semicolon or empty, if it is I don't want that line, if not then
> I want to append it to DataRead.

Try

   if strings[i][1] != ';' and strings[i][1] != ' ' then

alternatively

   if find(strings[i][1],"; ") = 0 then

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

3. Re: Testing for a empty string

Rich Klender wrote:
> 
> I finally got my function to run with no errors (yeah!) but I think I'm still
> doing something wrong.  My code:
> 
> for i = 1 to Lines do           
>         -- Begin a loop that reads in each line from the text file
>         --then, if strings[i] does not equal ; (a comment line) or
>         --"" (a blank line), it appends the data into the sequence
>         --DataRead
>         strings[i] = gets(FileName)
>         
>         if strings[i][1] != {";"} or {" "} then
>             DataRead = append ({}, strings[i])
>         end if
>     end for 
> 
> I want to test the 1st character of the line I received from the gets to see
> if it's a semicolon or empty, if it is I don't want that line, if not then
> I want to append it to DataRead.  Is the above correct?
> 
> Thanks!
> Rich
For one, you may want to test whether gets() is returning an atom or not (which
would indicate end of file). Plus you want to check for a newline which would
indicate an empty line. Here's how I would do it:

object instring -- may be defined elsewhere

for i = 1 to Lines do
        -- Begin a loop that reads in each line from the text file
        --then, if strings[i] does not equal ; (a comment line) or
        --"" (a blank line), it appends the data into the sequence
        --DataRead

        instring = gets(FileName)
        if atom(instring) then break -- something wrong here...
        elsif find(instring[1], ";\n") = 0 then -- check for ; and newline
            DataRead &= instring -- append non-comment, non-blank lines

        end if
        strings[i] = instring -- I don't know if this is needed or not...
end for 
}}}
<eucode>

Hmm, looking at your line here:
}}}
<eucode>
         if strings[i][1] != {";"} or {" "} then

What is the purpose of wrapping ; and space in braces and double quotes? You
probably want to use just single quotes and no braces (and check for newline).
Oh, and or doesn't work that way. And I changed "or" to "and". That's why I used
match up above in my version.

if strings[i][1] != ';' and strings[i][1] != ' ' and strings[i][1] !=
         '\n' then


I don't know if you need to check for a leading space or not so I took that out.
Hopefully my code makes sense to you and I'll be glad to answer any questions.

An alternative to my find() line above would be:
if instring[1] != ';' and instring[1] != '\n'

which may be clearer.

--
"Any programming problem can be solved by adding a level of indirection."
--anonymous
"Any performance problem can be solved by removing a level of indirection."
--M. Haertel
"Premature optimization is the root of all evil in programming."
--C.A.R. Hoare
j.

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

4. Re: Testing for a empty string

Perfect, thanks alot for the help.  Sometimes you know what you need just
not how to get it!

Thanks for the lesson!!  Please forgive the feeble attempt, I like to try it
on my own 1st and if that doesn't work, ask a expert!!

Rich

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

Search



Quick Links

User menu

Not signed in.

Misc Menu