Behavior of 'split' command 
Author Message
 Behavior of 'split' command

Hi:
I am more than a little confused about the output of the split command when
reading in lines from a text file.  What I am attempting to do is read in
lines of text from a file and make each line entry a separate list in the
list such that the result looks like this:
theList { {Joe:Doe:Engineer} {Jane:Doe:Scientist} }.  The format of the file
containg the data to be read is:

Joe:Doe:Engineer
Jane:Doe:Scientist

Each entry is ended by a newline.

Assume the file was opened sucessfully.
Here is my code for adding these list elements:

lappend theList [split  [read $theListFile]  \n]

What I expected was { {Joe:Doe:Engineer}
                                     {Jane:Doe:Scientist} }

What was returned was { Joe: Doe Engineer
                                       Jane:Doe:Scientist }

Reading the man pages and the leading Tcl books I have come to the
understanding that my code should have produced what I expected.  Can
someone enlighten me about how the split command should work and what I am
missing to produce my expected output?

Thank you



Sat, 03 Jun 2006 22:41:17 GMT  
 Behavior of 'split' command

Quote:

>  Hi:
>  I am more than a little confused about the output of the split command when
>  reading in lines from a text file.  What I am attempting to do is read in
>  lines of text from a file and make each line entry a separate list in the
>  list such that the result looks like this:
>  theList { {Joe:Doe:Engineer} {Jane:Doe:Scientist} }.  The format of the file
>  containg the data to be read is:

>  Joe:Doe:Engineer
>  Jane:Doe:Scientist

>  Each entry is ended by a newline.

>  Assume the file was opened sucessfully.
>  Here is my code for adding these list elements:

>  lappend theList [split  [read $theListFile]  \n]

>  What I expected was { {Joe:Doe:Engineer}
>                                       {Jane:Doe:Scientist} }

>  What was returned was { Joe: Doe Engineer
>                                         Jane:Doe:Scientist }

>  Reading the man pages and the leading Tcl books I have come to the
>  understanding that my code should have produced what I expected.  Can
>  someone enlighten me about how the split command should work and what I am
>  missing to produce my expected output?

It sounds like your confused about the difference between strings and
lists.

For instance
    set aString {{Joe:Doe:Engineer} {Jane:Doe:Scientist}}
    lappend aList {Joe:Doe:Engineer} {Jane:Doe:Scientist}

    puts $aString ;#==> {Joe:Doe:Engineer} {Jane:Doe:Scientist}
    puts $aList   ;#==> Joe:Doe:Engineer Jane:Doe:Scientist

Perhaps someone else can follow up with more detail...

--
Glenn Jackman
NCF Sysadmin



Sat, 03 Jun 2006 23:36:20 GMT  
 Behavior of 'split' command


Quote:

>>  Hi:
>>  I am more than a little confused about the output of the split command when
>>  reading in lines from a text file.  What I am attempting to do is read in
>>  lines of text from a file and make each line entry a separate list in the
>>  list such that the result looks like this:
>>  theList { {Joe:Doe:Engineer} {Jane:Doe:Scientist} }.  The format of the file
>>  containg the data to be read is:

>>  Joe:Doe:Engineer
>>  Jane:Doe:Scientist

>>  Each entry is ended by a newline.

>>  Assume the file was opened sucessfully.
>>  Here is my code for adding these list elements:

>>  lappend theList [split  [read $theListFile]  \n]

>>  What I expected was { {Joe:Doe:Engineer}
>>                                       {Jane:Doe:Scientist} }

>>  What was returned was { Joe: Doe Engineer
>>                                         Jane:Doe:Scientist }

>>  Reading the man pages and the leading Tcl books I have come to the
>>  understanding that my code should have produced what I expected.  Can
>>  someone enlighten me about how the split command should work and what I am
>>  missing to produce my expected output?

>It sounds like your confused about the difference between strings and
>lists.

>For instance
>    set aString {{Joe:Doe:Engineer} {Jane:Doe:Scientist}}
>    lappend aList {Joe:Doe:Engineer} {Jane:Doe:Scientist}

>    puts $aString ;#==> {Joe:Doe:Engineer} {Jane:Doe:Scientist}
>    puts $aList   ;#==> Joe:Doe:Engineer Jane:Doe:Scientist

>Perhaps someone else can follow up with more detail...

                        .
                        .
                        .
Summary:  I expect that Mr. Wilson will eventually decide he wants
simply
  set theList [split [read $theListFile] \n]

Glenn's $aString can be interpreted as a list; as a list, it's
indistinguishable from $aList.  Try
  puts [lindex $aString 0]
  puts [lindex $aList 0]

Is it really true that "What was returned was
  { Joe: Doe Engineer Jane:Doe:Scientist }
"?  Does the external file truly have a line
  Joe: Doe Engineer
rather than
  Joe:Doe:Engineer
?

--


Business:  http://www.Phaseit.net



Sun, 04 Jun 2006 00:00:24 GMT  
 Behavior of 'split' command
Hi:
Thanks for the responses.  I did have a typo in my original post.  All the
values in the external file are separated by a colon and the values returned
should have reflected that.
Thank you.


Quote:



> >>  Hi:
> >>  I am more than a little confused about the output of the split command
when
> >>  reading in lines from a text file.  What I am attempting to do is read
in
> >>  lines of text from a file and make each line entry a separate list in
the
> >>  list such that the result looks like this:
> >>  theList { {Joe:Doe:Engineer} {Jane:Doe:Scientist} }.  The format of
the file
> >>  containg the data to be read is:

> >>  Joe:Doe:Engineer
> >>  Jane:Doe:Scientist

> >>  Each entry is ended by a newline.

> >>  Assume the file was opened sucessfully.
> >>  Here is my code for adding these list elements:

> >>  lappend theList [split  [read $theListFile]  \n]

> >>  What I expected was { {Joe:Doe:Engineer}
> >>                                       {Jane:Doe:Scientist} }

> >>  What was returned was { Joe: Doe Engineer
> >>                                         Jane:Doe:Scientist }

> >>  Reading the man pages and the leading Tcl books I have come to the
> >>  understanding that my code should have produced what I expected.  Can
> >>  someone enlighten me about how the split command should work and what
I am
> >>  missing to produce my expected output?

> >It sounds like your confused about the difference between strings and
> >lists.

> >For instance
> >    set aString {{Joe:Doe:Engineer} {Jane:Doe:Scientist}}
> >    lappend aList {Joe:Doe:Engineer} {Jane:Doe:Scientist}

> >    puts $aString ;#==> {Joe:Doe:Engineer} {Jane:Doe:Scientist}
> >    puts $aList   ;#==> Joe:Doe:Engineer Jane:Doe:Scientist

> >Perhaps someone else can follow up with more detail...
> .
> .
> .
> Summary:  I expect that Mr. Wilson will eventually decide he wants
> simply
>   set theList [split [read $theListFile] \n]

> Glenn's $aString can be interpreted as a list; as a list, it's
> indistinguishable from $aList.  Try
>   puts [lindex $aString 0]
>   puts [lindex $aList 0]

> Is it really true that "What was returned was
>   { Joe: Doe Engineer Jane:Doe:Scientist }
> "?  Does the external file truly have a line
>   Joe: Doe Engineer
> rather than
>   Joe:Doe:Engineer
> ?

> --


> Business:  http://www.Phaseit.net



Sun, 04 Jun 2006 01:43:02 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. 'split' creates extra output

2. Splitting 'and' conditions into multiple conditions

3. splitting using '\' as a delimiter

4. newbie: emacs 'split-string'

5. Splitting a string every 'n'

6. delimiter '::' with split

7. CW4 - Bug or just 'unexpected behavior'?? :-)

8. SCM: behavior of 'delay'

9. Behavior of 'package'

10. 'CHAIN' command under WIN95

11. Substitute for VB command LoadImage('filename')

12. 'COPY TO' command

 

 
Powered by phpBB® Forum Software