Hello I am trying to use TCL in a program called Shorthand which is a text macro program.
Anyway I am trying to use it to stip and reformat Mac Addresses. I can not figure out the function to split a string at a specified index integer. Here is what I have so far.
set DirtyMac [sh_set clipboard]
set HalfMac [string map { . "" } $DirtyMac ]
set FullMac [string map { : "" } $HalfMac]
set ParsedMac [string map { " " "" } $FullMac]
if {[string length $ParsedMac] < 12 } {
return "Mac too short";
} elseif {[string length $ParsedMac] > 12 } {
return "Mac too long";
} else {
return [string map { " " "" } $ParsedMac]
}
That is to clean the Mac of all unwanted characters
I have been trying
set Mac1 [linsert $ParsedMac 4 .]
return $Mac1;
or
set Mac1 [split [string is index $ParsedMac 4 ] "."]
return $Mac1;
Nothing is working… anything anyone can do to help would be great!
Ok so what I am trying to do is take a Mac address such as 11:22:33:44:55:66 or 1122.3344.5566 or 112233445566 and have the output be in one of the those 3 formats. I will ultimately write 3 different scripts that will be called based on the need of the output.
So for example
I have the mac 1122.3344.5566 I want to turn it into 11:22:33:44:55:66
set DirtyMac "1122.3344.5566"
set ParsedMac [string map {. "" : "" " " ""} $DirtyMac]
scan $ParsedMac "%2s%2s%2s%2s%2s%2s" a b c d e f
set FullMac [join $a $b $c $d $e $f ":"]
return $FullMac;
OR
I have the mac 112233445566 I want to turn it into 1122.3344.5566
set DirtyMac "112233445566"
set ParsedMac [string map {. "" : "" " " ""} $DirtyMac]
scan $ParsedMac "%2s%2s%2s%2s%2s%2s" a b c d e f
set FullMac [join $a $b $c $d $e $f ":"]
return $FullMac;
lastly
I have the mac 1122.3344.5566 || 11:22:33:44:55:66 I want to turn it into 112233445566
set DirtyMac | Mac Address Input|
set ParsedMac [string map {. "" : "" " " ""} $DirtyMac]
return $ParsedMac;
First off, you can do the cleaning as a single step:
Mind you, I’d actually be tempted to use a different cleaning method: removing all non-hex characters:
Secondly, splitting that string into 4-character pieces (note that I’m assuming you put the length checks in first) is actually best done with
scan:This extracts the four four-hex-digit characters and converts them into numbers. Alternatively, you can scan them as ordinary strings:
Then you’d need to do whatever you are looking to do with putting the pieces back together.