Zuckerberg
Zuckerberg

Reputation: 205

Split the following list into 4 elements using TCL

How Do I split the following list into 4 elements using TCL

W.1.Agg-251::ethernet17/24 Z.1.Acc-2::ethernet17/1

I tried as below , But the middle two elements looks to be sticking together ,

set c [split $c ::] {\ W.1.Agg-251 {} {ethernet17/24 Z.1.Acc-2} {} ethernet17/1}

Update:

The solution provided below does work if I pass the list as it is , but when i pass it as a variable liek below , again I see the middle elements are stuck together .

Like so :

 set list2 [lindex $list 0]

o/p==> W.1.Agg-251::ethernet17/24 Z.1.Acc-2::ethernet17/1

 set list3 [split [string map {:: :} $list2] ":" ]

o/p==> { W.1.Agg-251} {ethernet17/24 Z.1.Acc-2} ethernet17/1

Upvotes: 0

Views: 2072

Answers (3)

Hai Vu
Hai Vu

Reputation: 40688

All you need to do is to replace "::" with a space " ":

set list2 [string map {"::" " "} $list]

Verify:

foreach token $list2 { puts ">$token<" }

Output:

>W.1.Agg-251<
>ethernet17/24<
>Z.1.Acc-2<
>ethernet17/1<

Upvotes: 0

patthoyts
patthoyts

Reputation: 33193

The second parameter to split is not a string to split on but a set of characters that may be used for splitting. Note the following example:

% split {a::b::c::d} ::
a {} b {} c {} d
% split {a::b::c::d} :
a {} b {} c {} d

What you are trying to do seems to be to split on "::" and space. You should squash the :: sequence first, then split as below:

% split [string map {:: :} {W.1.Agg-251::ethernet17/24 Z.1.Acc-2::ethernet17/1}] ": "
W.1.Agg-251 ethernet17/24 Z.1.Acc-2 ethernet17/1

Upvotes: 1

Foo
Foo

Reputation: 462

You said 4 elements, but I only see 3 elements (two :: pairs). Are you sure the space between 24 and Z should not be a :: instead?

Upvotes: 0

Related Questions