How to get value of second element from list in tcl? - tcl

i have tcl list
data = {{"a" "1"} {"b" "2"}} {x y}}
now when am doing -
set str [lindex $data 1]
am getting values-
x y
How i can get values 1 2 ?
i tried using
set str [lindex $data 2]
but am getting null values?

If you have:
set data {{{"a" "1"} {"b" "2"}} {x y}}
Then you can extract 1 2 with:
set extracted [lmap pair [lindex $data 0] {lindex $pair 1}]
This is a bit messy because we want to extract a sublist to process and then do an element extraction for each element of that sublist; hence the two lindex calls and the lmap.
If the data had instead been:
set data {{"a" "1"} {"b" "2"} {x y}}
Then we'd be looking at perhaps:
set extracted [lmap pair [lrange $data 0 end-1] {lindex $pair 1}]
The main idea is still lmap and lindex, but we do a range selection first. These techniques combine; it really depends on the data (and it is often better to try to ensure things aren't so complicated in the first place, if possible).
If you had:
set data {{"a" "1"} {"b" "2"} {"c" "3"}}
Then the extraction is the simplest case:
set extracted [lmap pair $data {lindex $pair 1}]

Related

Addition or Subtraction of a number from each element of a list using Tcl Script

I have a input file name "input.dat" with the values as:
7 0
9 9
0 2
2 1
3 4
4 6
5 7
5 6
And I want to add/subtract any number from column 2 by converting it into a list using Tcl Script. I have written the Tcl Script as follows:
set input [open "input.dat" r]
set data [read $input]
set values [list]
foreach line [split $data \n] {
if {$line eq ""} {break}
lappend values [lindex [split $line " "] 1]
}
puts "$values-2"
close $input
But the output comes out to be: 0 9 2 1 4 6 7 6-2
Can anybody help me, how to fix this problem ? or what is the error in the script ? It's also helpful if anybody can help me with a correct script.
I'm still not 100% sure what you want, but the options all seem to be solvable with the lmap command, which is for applying an operation to each element of a list.
Here's how to concatenate each element with -2:
set values [lmap val $values {
string cat $val "-2"
}]
Here's how to subtract 2 from each element:
set values [lmap val $values {
expr {$val - 2}
}]
puts will treat it as a string, you'll have to use [expr $val - 2]
NOTE: If it doesn't work, it is possible your input list is a string not int or float (Depends on how the values were read). In this case you can use:
scan $val %d tmp
set newval [expr $tmp - 2]
puts $newval
This will convert your string to int before applying mathematical expressions. You can similarly convert to float by using %f in scan instead of %d

Parse CSV into key value pair

I have a csv file which has hostname and attached serial numbers. I want to create a key value pair with key being hostname and value being the list of serial numbers. The serial numbers can be one or many.
For example:
A, 1, 2, 3, 4
B, 5, 6
C, 7, 8, 9
D, 10
I need to access key A and get {1 2 3 4} as output. And if I access D i should get {10}
How should I do this? As the version of TCL i am using doesn't support any packages like CSV and I also won't be able to install it as it is in the server, So I am looking at a solution which doesn't include any packages.
For now, I am splitting the line with \n and then I process each element. Then I split the elements with "," and then I get the host name and serial numbers in a list. I then use the 0th index of the list as hostname and remaining values as serial numbers. Is there a cleaner solution?
I'd do something like:
#!/usr/bin/env tclsh
package require csv
package require struct::queue
set filename "file.csv"
set fh [open $filename r]
set q [struct::queue]
csv::read2queue $fh $q
close $fh
set data [dict create]
while {[$q size] > 0} {
set values [lassign [$q get] hostname]
dict set data $hostname [lmap elem $values {string trimleft $elem}]
}
dict for {key value} $data {
puts "$key => $value"
}
then
$ tclsh csv.tcl
A => 1 2 3 4
B => 5 6
C => 7 8 9
D => 10
The repeated recommendation given here is to use the CSV package for this purpose. See also the answer by #glenn-jackman. If unavailable, the time is better invested in obtaining it at your server side.
To get you started, however, you might want to adopt something along the lines of:
set dat {
A, 1, 2, 3, 4
B, 5, 6
C, 7, 8, 9
D, 10
}
set d [dict create]
foreach row [split [string trim $dat] \n] {
set row [lassign [split $row ,] key]
dict set d [string trim $key] [concat {*}$row]
}
dict get $d A
dict get $d D
Be warned, however, such hand-knitted solutions typically only serve their purpose when you have full control of the data being processed and its representation. Again, time is better invested by obtaining the CSV package.
I tried this way and got it working. Thanks again for your inputs. Yes, I know csv package would be easy but I cannot install it in server/product.
set multihost "host_slno.csv"
set fh1 [open $multihost r]
set data [read -nonewline $fh1]
close $fh1
set hostslnodata [ split $data "\n" ]
set hostslno [dict create];
foreach line $hostslnodata {
set line1 [join [split $line ", "] ]
puts "$line1"
if {[regexp {([A-Za-z0-9_\-]+)\s+(.*)} $line1 match hostname serial_numbers]} {
dict lappend hostslno $hostname $serial_numbers
}
}
puts [dict get $hostslno]
The sourcecode from the csv package is available. If you are unable to install the full csv package, you can include the code from here:
http://core.tcl.tk/tcllib/artifact/2898cd911697ecdb
If you still can't use that option, then stripping out all the whitespace and splitting on "," is required.
An alternative to the earlier answers is using string map:
set row [split [string map {" " ""} $row ] ,]
The string map will remove all spaces, and then split on ","
Once you have converted the lines of text into valid tcl lists:
A 1 2 3 4
B 5 6
C 7 8 9
D 10
Then you can use the lindex and lrange commands to pluck off all the pieces.
foreach row $data {
set server [lindex $row 0]
set serial_numbers [lrange $row 1 end]
dict set ...
One possibility:
set hostslno [dict create]
set multihost "host_slno.csv"
set fh1 [open $multihost]
while {[gets $fh line] >= 0} {
set numbers [lassign [regexp -inline -all {[^\s,]+} $line] hostname]
dict set hostslno $hostname $numbers
}
close $fh1
puts [dict get $hostslno A]

how to split a file to list of lists TCL

I'm coding TCL and I would like to split a file into two lists of lists,
the file contain:
(1,2) (3,4) (5,6)
(7,8) (9,10) (11,12)
and I would like to get two list
one for each line, that contain lists that each one contain to two number
for example:
puts $list1 #-> {1 2} {3 4} {5 6}
puts [lindex $list1 0] #-> 1 2
puts [lindex $list2 2] #-> 11 12
I tried to use regexp and split but no success
The idea of using regexp is good, but you'll need to do some post-processing on its output.
# This is what you'd read from a file
set inputdata "(1,2) (3,4) (5,6)\n(7,8) (9,10) (11,12)\n"
foreach line [split $inputdata "\n"] {
# Skip empty lines.
# (I often put a comment format in my data files too; this is where I'd handle it.)
if {$line eq ""} continue
# Parse the line.
set bits [regexp -all -inline {\(\s*(\d+)\s*,\s*(\d+)\s*\)} $line]
# Example results of regexp:
# (1,2) 1 2 (3,4) 3 4 (5,6) 5 6
# Post-process to build the lists you really want
set list([incr idx]) [lmap {- a b} $bits {list $a $b}]
}
Note that this is building up an array; long experience says that calling variables list1, list2, …, when you're building them in a loop is a bad idea, and that an array should be used, effectively giving variables like list(1), list(2), …, as that yields a much lower bug rate.
An alternate approach is to use a simpler regexp and then have scan parse the results. This can be more effective when the numbers aren't just digit strings.
foreach line [split $inputdata "\n"] {
if {$line eq ""} continue
set bits [regexp -all -inline {\([^()]+\)} $line]
set list([incr idx]) [lmap substr $bits {scan $substr "(%d,%d)"}]
}
If you're not using Tcl 8.6, you won't have lmap yet. In that case you'd do something like this instead:
foreach line [split $inputdata "\n"] {
if {$line eq ""} continue
set bits [regexp -all -inline {\(\s*(\d+)\s*,\s*(\d+)\s*\)} $line]
set list([incr idx]) {}
foreach {- a b} $bits {
lappend list($idx) [list $a b]
}
}
foreach line [split $inputdata "\n"] {
if {$line eq ""} continue
set bits [regexp -all -inline {\([^()]+\)} $line]
set list([incr idx]) {}
foreach substr $bits {
lappend list($idx) [scan $substr "(%d,%d)"]
# In *very* old Tcl you'd need this:
# scan $substr "(%d,%d)" a b
# lappend list($idx) [list $a $b]
}
}
You have an answer already, but it can actually be done a little bit simpler (or at least without regexp, which is usually a good thing).
Like Donal, I'll assume this to be the text read from a file:
set lines "(1,2) (3,4) (5,6)\n(7,8) (9,10) (11,12)\n"
Clean it up a bit, removing the parentheses and any white space before and after the data:
% set lines [string map {( {} ) {}} [string trim $lines]]
1,2 3,4 5,6
7,8 9,10 11,12
One way to do it with good old-fashioned Tcl, resulting in a cluster of variables named lineN, where N is an integer 1, 2, 3...:
set idx 0
foreach lin [split $lines \n] {
set res {}
foreach li [split $lin] {
lappend res [split $li ,]
}
set line[incr idx] $res
}
A doubly iterative structure like this (a number of lines, each having a number of pairs of numbers separated by a single comma) is easy to process using one foreach within the other. The variable res is used for storing result lines as they are assembled. At the innermost level, the pairs are split and list-appended to the result. For each completed line, a variable is created to store the result: its name consists of the string "line" and an increasing index.
As Donal says, it's not a good idea to use clusters of variables. It's much better to collect them into an array (same code, except for how the result variable is named):
set idx 0
foreach lin [split $lines \n] {
set res {}
foreach li [split $lin] {
lappend res [split $li ,]
}
set line([incr idx]) $res
}
If you have the results in an array, you can use the parray utility command to list them in one fell swoop:
% parray line
line(1) = {1 2} {3 4} {5 6}
line(2) = {7 8} {9 10} {11 12}
(Note that this is printed output, not a function return value.)
You can get whole lines from this result:
% set line(1)
{1 2} {3 4} {5 6}
Or you can access pairs:
% lindex $line(1) 0
1 2
% lindex $line(2) 2
11 12
If you have the lmap command (or the replacement linked to below), you can simplify the solution somewhat (you don't need the res variable):
set idx 0
foreach lin [split $lines \n] {
set line([incr idx]) [lmap li [split $lin] {
split $li ,
}]
}
Still simpler is to let the result be a nested list:
set lineList [lmap lin [split $lines \n] {
lmap li [split $lin] {
split $li ,
}
}]
You can access parts of the result similar to above:
% lindex $lineList 0
{1 2} {3 4} {5 6}
% lindex $lineList 0 0
1 2
% lindex $lineList 1 2
11 12
Documentation:
array,
foreach,
incr,
lappend,
lindex,
lmap (for Tcl 8.5),
lmap,
parray,
set,
split,
string
The code works for windows :
TCL file code is :
proc captureImage {} {
#open the image config file.
set configFile [open "C:/main/image_config.txt" r]
#To retrive the values from the config file.
while {![eof $configFile]} {
set part [split [gets $configFile] "="]
set props([string trimright [lindex $part 0]]) [string trimleft [lindex $part 1]]
}
close $configFile
set time [clock format [clock seconds] -format %Y%m%d_%H%M%S]
set date [clock format [clock seconds] -format %Y%m%d]
#create the folder with the current date
set folderPath $props(folderPath)
append folderDate $folderPath "" $date "/"
set FolderCreation [file mkdir $folderDate]
while {0} {
if { [file exists $date] == 1} {
}
break
}
#camera selection to capture image.
set camera "video"
append cctv $camera "=" $props(cctv)
#set the image resolution (XxY).
set resolutionX $props(resolutionX)
set resolutionY $props(resolutionY)
append resolution $resolutionX "x" $resolutionY
#set the name to the save image
set imagePrefix $props(imagePrefix)
set imageFormat $props(imageFormat)
append filename $folderDate "" $imagePrefix "_" $time "." $imageFormat
set logPrefix "Image_log"
append logFile $folderDate "" $logPrefix "" $date ".txt"
#ffmpeg command to capture image in background
exec ffmpeg -f dshow -benchmark -i $cctv -s $resolution $filename >& $logFile &
after 3000
}
}
captureImage
thext file code is :
cctv=Integrated Webcam
resolutionX=1920
resolutionY=1080
imagePrefix=ImageCapture
imageFormat=jpg
folderPath=c:/test/
//camera=video=Integrated Webcam,Logitech HD Webcam C525
This code works for me me accept the code from text file were list of parameters are passed.

How to convert a tcl variable value to specific format

how can I convert
set var "USE_90a_Sc_ttv"
to
set out "9.0a ttv Sc"
using tcl code?
Regards,
Divesh
Use the split, lassign and regsub functions:
lassign [split $var _] prefix version type tag
regsub {(\d(\w)?)$} $version {.\1} nversion
set out "$nversion $tag $type"
If you are using an older version and don't have lassign available, you
can use lindex to retrieve specific items from the list returned by split.
set tlist [split $var _]
set version [lindex $tlist 1]
set type [lindex $tlist 2]
set tag [lindex $tlist 3]
regsub {(\d(\w)?)$} $version {.\1} nversion
set out "$nversion $tag $type"
I'd use scan to parse that, and list to assemble the results.
set var "USE_90a_Sc_ttv"
# Remember to check the result of [scan] for number of parsed fields
if {[scan $var {USE_%1d%2[^_]_%2[^_]_%3s%c} a b c d e] != 4} {
error "Unexpected input data! '$var'"
}
set out [list $a.$b $d $c]
Putting a %c at the end of the format lets me detect if there are any unexpected characters at the end. There shouldn't be; only 4 fields ought to be satisfied. This makes for a quick way to check that what I've got is what I expect. Also, %2[^_] is an unusual field specifier, but all it does is ask for 2 non-underscore characters.

TCL getting the nth item from every list that matches in a list of lists

I am looking for a nice short way to get every nth item from a list of lists if the nested list matches a criteria.
So if I have this list:
set x [list [list a 1] [list b 2] [list a 3] [list b 4]]
looking for all the second items in the lists that has "a" as the first item
I want to get {1 3}.
(The list is a key-value pair, so in short I want all the values of the specified key).
This does the work:
lsearch -all -index 1 -inline -subindices [lsearch -all -index 0 -inline $x a] *
But I am looking for a neater shorter way to do this.
Thanks!
With 8.5, I'd advise sticking with what you've got. With Tcl 8.6, you can use lmap:
lmap i $x {lassign $i k v; if {$k ne "a"} continue; set v}
lmap i $x {if {[lindex $i 0] ne "a"} continue {lindex $i 1}}
I'm not sure which one you prefer. (The second is a little longer and a little trickier, but generates better bytecode. The versions with lsearch aren't bytecoded in any version.)