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
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
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 :

Quote:
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:

Code:
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:

Code:
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
Hi,

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"

Say,

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/Firefox.app/Contents/MacOS/firefox-bin -psn_0_31195137
21517  ??  S      0:01.99 /Applications/Dictionary.app/Contents/MacOS/Dictionary -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"

Thanks
Fahad

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 www.opentalk.org.uk 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., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho