Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
how to split string w/variable # of spaces

Posted by doublea1535 (doublea1535), 16 September 2006
I have a string I want to turn into a list. I figure using split might be the best way. The string uses whitespace as a delimiter, but not necessarily just one whitespace character. Here an example string :

5/0  ATM       2.320  BOUND  PPP         subscribr  12346@isp

Depending on the actual data, the amount of whitespace between two fields will change.

Posted by admin (Graham Ellis), 17 September 2006
A list is just a text string in a certain format - space separated fields, etc ... so in the case of your example you don't need split or anything - just treat the string as a list:

set source "5/0  ATM  2.320  BOUND  PPP    subscribr  12346@isp"
foreach s_el $source {
       puts $s_el

If, however, your data could contain special characters such as { [ " and \, you'll need to use split or something similar. With multiple white spaces, you can get rid of them first using regsub to squash them down to a single space:

set source "5/0  ATM  2.320  BOUND  PPP    subscribr  12346@isp"
regsub -all -- {[[:space:]]+} $source " " source
set source [split $source]
foreach s_el $source {
       puts $s_el

Posted by shafa.fahad (FahaD), 18 September 2006

I was about to post a topic for How to split the variable by "Newline", since I found this topic to be related I am posting here itself....

I would like to know how to split a variable by "Newline"


I have written script like...

     send "ps -ef|grep $arg1\r"
     expect -re $prompt
     set VAL $expect_out(buffer)

the value of VAL woule be a multi line output like

20883  ??  S     48:16.52 /Applications/ -psn_0_31195137
21517  ??  S      0:01.99 /Applications/ -psn_0_31981569
23647  ??  SNs    0:00.22 /System/Library/Frameworks/CoreServices.framework/Metadatamdimportserver
23754  ??  SNs    0:00.26 /System/Library/Frameworks/CoreServices.framework/Metadatamdimportserver

I would like to know How to split this output by "line"


Posted by admin (Graham Ellis), 18 September 2006
set lines [split $VAL "\n"]
should do it, I think.

This page is a thread posted to the opentalk forum at and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2015: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: • WEB: • SKYPE: wellho