how to split each new line into a list, considering quoted new line characters 
Author Message
 how to split each new line into a list, considering quoted new line characters

Splitting each new line into a separate element of a list is done easily
by the [split] command. If you want to ignore quoted newline characters
the [split] command does not work. Is there a simple method to do the
job?

Example:

set string {
  a b {
    ignore these
    lines
  }
  c d e

Quote:
}

specialSplit $string \n

=> {} {  a b {
    ignore these
    lines
  } {  c d e} {}



Sun, 27 Feb 2005 19:27:13 GMT  
 how to split each new line into a list, considering quoted new line characters

Quote:

> Splitting each new line into a separate element of a list is done easily
> by the [split] command. If you want to ignore quoted newline characters
> the [split] command does not work. Is there a simple method to do the
> job?

> Example:

> set string {
>   a b {
>     ignore these
>     lines
>   }
>   c d e
> }

> specialSplit $string \n

> => {} {  a b {
>     ignore these
>     lines
>   } {  c d e} {}

This will take a little parser - but if you only need
the case of newlines and tcl style quoting blocks, you
can cheat and use the tcl parser instead of writing your
own

proc specialSplit {str sep} {
   set ret {}
   set s ""
   foreach chunk [split $str $sep] {
     if {[string length $s]>0} {
       append s "$sep$chunk"
     } else {
       set s $chunk
     }
     if {[info complete $s]} {
       lappend ret $s
       set s ""
     }
   }
   return $ret

Quote:
}

Bruce


Sun, 27 Feb 2005 21:29:53 GMT  
 how to split each new line into a list, considering quoted new line characters

Quote:

> This will take a little parser - but if you only need
> the case of newlines and tcl style quoting blocks, you
> can cheat and use the tcl parser instead of writing your
> own

> proc specialSplit {str sep} {
>    set ret {}
>    set s ""
>    foreach chunk [split $str $sep] {
>      if {[string length $s]>0} {
>        append s "$sep$chunk"
>      } else {
>        set s $chunk
>      }
>      if {[info complete $s]} {
>        lappend ret $s
>        set s ""
>      }
>    }
>    return $ret
> }

In Tcl it is all there. One must only find it.  :-)
It was the [info complete] command that I was looking for. Thank you.

Michael



Mon, 28 Feb 2005 17:25:47 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. new question in how can i read data from file line by line

2. How to Replace a New Line Character

3. Tabs and new line characters

4. New Line Character

5. New line character

6. sml new line character

7. Detecting new line character inside string

8. New Line or new Paragraph

9. End of line character in SPLIT command

10. how to list elements line by line?

11. New Line?

12. Removing new line .

 

 
Powered by phpBB® Forum Software