1. Parsing with StrTok

I'm trying to do this:

   temp = parse( list, "\n," )

but it doesn't work with the '\n'. How do I parse on a 'newline?'

I've also tried

   temp = parse( list, '\n' & ',' )

This does work:

   temp = parse( list, "," )

-=ck
"Programming in a state of EUPHORIA."
http://www.cklester.com/euphoria/

new topic     » topic index » view message » categorize

2. Re: Parsing with StrTok

cklester wrote:
> 
> I'm trying to do this:
> 
>    temp = parse( list, "\n," )
> 
> but it doesn't work with the '\n'. How do I parse on a 'newline?'
> 
> I've also tried
> 
>    temp = parse( list, '\n' & ',' )
> 
> This does work:
> 
>    temp = parse( list, "," )

Are there perhaps some '\r's in there?

Matt Lewis

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

3. Re: Parsing with StrTok

Matt Lewis wrote:
> cklester wrote:
> > I'm trying to do this:
> >    temp = parse( list, "\n," )
> > but it doesn't work with the '\n'. How do I parse on a 'newline?'
> > I've also tried
> >    temp = parse( list, '\n' & ',' )
> > This does work:
> >    temp = parse( list, "," )
> Are there perhaps some '\r's in there?

I'm actually getting an error message:

./Strtok-v2-1.e:293 in function parse() 
type_check failure, params is 44',' 

???

-=ck
"Programming in a state of EUPHORIA."
http://www.cklester.com/euphoria/

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

4. Re: Parsing with StrTok

C.K.,

what do you *exactly* want to achieve?
',' as well as '\n' should be recognized as delimiter in your list,
right? And do you want to preserve *empty* fields between the delimiters?
I.e. do you want to be "apples,oranges,,bananas" a list of 3 or of 4
items?

Regards,
   Juergen

-- 
Have you read a good program lately?

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

5. Re: Parsing with StrTok

Juergen Luethje wrote:
> 
> what do you *exactly* want to achieve?

I've got a string that will be delimited by commas and/or line breaks.
Kat's strtok's parse() routine seems to allow for multiple delimiters, but
it doesn't seem to be working with '\n'. Got a fix? Kat?

The final list needs to be in a sequence, of course.

"one,two
three"

becomes { "one","two","three" }


-=ck
"Programming in a state of EUPHORIA."
http://www.cklester.com/euphoria/

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

6. Re: Parsing with StrTok

cklester wrote:

> Juergen Luethje wrote:
>>
>> what do you *exactly* want to achieve?
>
> I've got a string that will be delimited by commas and/or line breaks.
> Kat's strtok's parse() routine seems to allow for multiple delimiters, but
> it doesn't seem to be working with '\n'. Got a fix? Kat?
>
> The final list needs to be in a sequence, of course.
>
> "one,two
> three"
>
> becomes { "one","two","three" }

OK. In order to be able to help you, I must know the answer to the other
question that I asked. What do you want "one,two,,more" to become? Note
the two adjacent commas.
     ->  { "one","two","more" }
or
     ->  { "one","two","","more" }
?

I don't have a fix for strtok's parse(), but I can give you another
function that does the job.

Regards,
   Juergen

-- 
Have you read a good program lately?

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

7. Re: Parsing with StrTok

Juergen Luethje wrote:
> cklester wrote:
> > Juergen Luethje wrote:
> >> what do you *exactly* want to achieve?
> > I've got a string that will be delimited by commas and/or line breaks.
> > Kat's strtok's parse() routine seems to allow for multiple delimiters, but
> > it doesn't seem to be working with '\n'. Got a fix? Kat?
> >
> > The final list needs to be in a sequence, of course.
> >
> > "one,two
> > three"
> >
> > becomes { "one","two","three" }
> 
> OK. In order to be able to help you, I must know the answer to the other
> question that I asked. What do you want "one,two,,more" to become? Note
> the two adjacent commas.
>      ->  { "one","two","more" }
> or
>      ->  { "one","two","","more" }

The first one. Empty sequences aren't needed.

-=ck
"Programming in a state of EUPHORIA."
http://www.cklester.com/euphoria/

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

8. Re: Parsing with StrTok

cklester wrote:

> Juergen Luethje wrote:

<snip>

>> What do you want "one,two,,more" to become?
>> Note the two adjacent commas.
>>      ->  { "one","two","more" }
>> or
>>      ->  { "one","two","","more" }
>
> The first one. Empty sequences aren't needed.

Maybe it can be done faster?
However, this is simple and straightforward:

-- slightly modified after code by Andy Serpa
-- (posted November 2004 on EUforum)

constant
   FALSE = 0, TRUE = not FALSE

type boolean (object x)
   if integer(x) then
      return x = FALSE or x = TRUE
   end if
   return FALSE
end type

function parse (sequence source, sequence delimList)
   -- in: source   : string to parse
   --     delimList: list of characters that are considered delimiters
   sequence parsed
   integer ptr
   boolean app

   parsed = {}
   ptr = 1
   app = FALSE
   for i = 1 to length(source) do
      if find(source[i], delimList) then
         if app = TRUE then
            parsed = append(parsed, source[ptr..i-1])
            app = FALSE
            ptr = i+1
         else
            ptr += 1
         end if
      else
         app = TRUE
      end if
   end for
   if app = TRUE then
      parsed = append(parsed, source[ptr..$])
   end if

   return parsed
end function


-- Demo
include misc.e
sequence source, delimList, parsed

source = "\rone,two\n,three,,\r\nfour,,,"
delimList = ",\r\n"
parsed = parse(source, delimList)
pretty_print(1, parsed, {3})


Regards,
   Juergen

-- 
Have you read a good program lately?

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

9. Re: Parsing with StrTok

Juergen Luethje wrote:
> 
> Maybe it can be done faster?
> However, this is simple and straightforward:

Beautiful, Juergen. Thanks! :)

-=ck
"Programming in a state of EUPHORIA."
http://www.cklester.com/euphoria/

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

Search



Quick Links

User menu

Not signed in.

Misc Menu