Forum Discussion
Splitting a delimited string into multiple variables in one line with TCL split versus GetField
This is a breeze to do in perl...
$string=="blah1-blah2-blah3";
($field1, $field2, $field3) = split(/-/,$string,3);
In TCL there is the split function, but I don't see any references to assigning the component variables in one line According the tcl.tk man page for split that uses an example of splitting of a passwd file, it seems this is the TCL answer to the PERL example above
set string "blah1-blah2-blah3"
set fields [split $string "-"]
foreach field $fields {
lassign $fields field1 field2 field3
}
Is there no one line way to both split and assign into scalar variables all at once? Is a foreach loop honestly necessary and then another command to do the assignment? Sigh, so tedious
Now on to GetField...this is what I have been using and currently I am stuck with having a separate GetField line of irule for each component field in my delimited scalar string. If there is a way to use GetField to pull and assign 2 or more values from a single delimited string please let me know how. Also what is GetField doing? Is it just an F5 shortcut/wrapper to TCL's split? I dont see GetField as part of the TCL documentation, but maybe I missed it
The background here is I am using a Proc to do some string work for HTTP_REQUEST and I need the proc to return a scalar delimited value that my HTTP_REQUEST event can then assign each into a variable. Right now I have to do multiple GetField calls on the string, one for each variable I need What is worse is I have a very nice embedded call to the proc inside like this
set newquery "?[getfield [call fully_decode_urx [HTTP::query] 7] "~~~" 2]"
But I certainly don't want to call the proc twice just to assign more variables and I am loathe to use a variable to get the delimited string back from the proc and then use two separate newline GetField calls to parse that string, sure at least that wouldn't call the proc twice, but the simplicity of the PERL split is really what I am looking for. If I can do that in TCL supported native or with GetField or some other way I'd love to know how
And of course since variable are locally scoped in a Proc I can't set them there and then reference them from inside HTTP_REQUEST
Appreciate any advice/suggestions
3 Replies
- John_Alam_45640Historic F5 Account
I don't think the foreach is needed.
This should be all you need.
set string "blah1-blah2-blah3" set fields [split $string "-"] lassign $fields field1 field2 field3
getfield is not TCL , it is iRules only.
- Kevin_Stewart
Employee
Not sure I completely understand, the result of a tcl split is a list:
set str "blah1-blah2-blah3" set fields [split $str "-"]
You can then use this list as is, completely indexable:
log local0. [lindex $fields 0] =blah1
The getfield command is indeed an iRule-optimized "lindex-split"
set str "blah1-blah2-blah" log local0. [lindex [split $str "-"] 1] =blah2
As far as proxy are concerned, a tcl list is really just an indexable string, so it's straight forward to return a list from a proc call:
proc test { var } { return [split $var "-"] } set str "blah1-blah2-blah3" set doit [call test $str] log local0. [lindex $doit 2] =blah3
- John_Alam_45640Historic F5 Account
(System32) 42 % set string "blah1-blah2-blah3" blah1-blah2-blah3 (System32) 43 % set fields [split $string "-"] blah1 blah2 blah3 (System32) 44 % lassign $fields field1 field2 field3 (System32) 45 % puts $field1 blah1 (System32) 46 % puts $field2 blah2 (System32) 47 % puts $field3 blah3 (System32) 48 %
Recent Discussions
Related Content
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com