I have this data
# Curve 0 of 2, 7 points
# x y xlow xhigh type
20.781 1 20.781 20.781
20.8102 10 20.8102 20.8102
20.8395 18 20.8395 20.8395
20.8687 13 20.8687 20.8687
20.898 15 20.898 20.898
20.9273 18 20.9273 20.9273
20.9565 13 20.9565 20.9565
# Curve 1 of 2, 7 points
# x y xlow xhigh type
21.635 2 21.635 21.635
21.6625 19 21.6625 21.6625
21.6899 29 21.6899 21.6899
21.7173 63 21.7173 21.7173
21.7447 137 21.7447 21.7447
21.7721 168 21.7721 21.7721
21.7996 109 21.7996 21.7996
All the information is an unique file, i.e block data are separated by three blank lines.
I want to collect the information that is just at the beginning of the next line that has characters # x y xlow xhigh type. Also I want to collect the information that is at the end of each block. In other words, I want to print on screen the values that are in bold letter (20.781 20.9565 21.635 21.7996).
I wrote these lines of code but I don't know how to print the info that is just below the characters # x y.
set input [open "dataHist.dat" r]
while { [gets $input line] != -1 } {
if { [string range 0 4] == "# x y"} {
}
}
Since the first line of each block tells you long it is, you can use that to tell which lines you want to extract the first number from:
#!/usr/bin/env tclsh
proc must_gets {ch var_} {
upvar $var_ var
if {[gets $ch var] < 0} {
error "Premature end of file"
}
}
proc extract_numbers {filename} {
set ch [open $filename]
try {
set nums {}
while {[gets $ch line] >= 0} {
if {[regexp {^# Curve \d+ of \d+, (\d+) points} $line -> nPoints]} {
must_gets $ch line ;# Discard '# x y ...' line.
must_gets $ch line ;# First point line
# Extract first element of it
lappend nums [lindex [split $line] 0]
# Read remaining point lines
for {set n 2} {$n <= $nPoints} {incr n} {
must_gets $ch line
}
# And extract first element of last one
lappend nums [lindex [split $line] 0]
}
}
return $nums
} finally {
chan close $ch
}
}
# 20.781 20.9565 21.635 21.7996
puts [extract_numbers dataHist.dat]
My test file contains :
2 server[0]/asm
3 server[1]/asm
5 server[1]/pst
6 server[0]/pst
3 server[2]/qrf
5 server[1]/qrf
.
.
and so on
I need results something as :
3 asm
6 pst
5 qrf
I have tried something like this till now:
set fp [open ./txt r]
set fw [open ./txt w]
while {[gets $fp line] >= 0} {
if {[regexp {\[0\]} $line]} {
puts $fw [string map {server\[0\]/ ""} $line]
} eslsif {[regexp {\[1\]} $line]} {
puts $fw [string map {server\[1\]/ ""} $line]
} elseif {[regexp {\[2\]} $line]} {
puts $fw [string map {server\[2\]/ ""} $line]
}
}
close $fp
close $fw
The output till now is:
2 asm
3 asm
5 pst
6 pst
3 qrf
5 qrf
There are only 3 servers. server[0], server [1] and server[2].
So i want maximum value from column 1 for the same last name in column 2 in tcl.
Please guide me through it.
I'd use a regular expression to pick out the parts of each line
set names [dict create]
while {[gets $f line] != -1} {
if {[regexp {^(\d+).*/(.*)$} $line -> num name]} {
dict lappend names $name $num
}
}
dict for {name nums} $names {
puts [list [tcl::mathfunc::max {*}$nums] $name]
}
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.
I wanted to read a file one line a time, split its columns and saved it in variables.
File looks like this:
%% cat Memory_minus.list
MEM_rf_2p_hce_1024x8_naxos1 RF dual_port 1024 8
MEM_rf_2p_hce_128x18_naxos1 RF dual_port 128 18
MEM_rf_2p_hce_16x128_fusion RF dual_port 16 128
MEM_rf_2p_hce_16x80_fusion4 RF dual_port 16 80
MEM_rf_2p_hce_256x18_naxos1 RF dual_port 256 18
MEM_rf_2p_hce_256x8_naxos1 RF dual_port 256 8
in a single foreach loop(one line) i wanted to have all 5 columns converted to variables, eg for first line each variable should return following.
puts $var1 ->>> MEM_rf_2p_hce_1024x8_naxos1
puts $var2 ->>> RF
puts $var3 ->>> dual_port
puts $var4 ->>> 1024
puts $var5 ->>> 8
I tried writing a code to read a file line by line and then extracting the items but it didn't worked.
set f [open Memory_minus.list]
while {1} {
set line [gets $f]
if {[eof $f]} {
close $f
break
}
#set base_name [exec awk {{print $1}} $line ]
foreach lt $line {
puts $lt
}
}
$lt return whole scrambled outputs.
set fp [open input.txt r]
while {[gets $fp line]!=-1} {
# To make sure that the line has five words
if {[llength $line]!=5} {
puts "Incorrect format in the line -> '$line'"
continue
}
foreach {var1 var2 var3 var4 var5} $line {
puts ->>>$var1;puts ->>>$var2;puts ->>>$var3;puts ->>>$var4;puts ->>>$var5;
}
}
close $fp
Output : (Showing only first line's output)
->>>MEM_rf_2p_hce_1024x8_naxos1
->>>RF
->>>dual_port
->>>1024
->>>8
I have a list which I am trying to modify and make a new list based on what I am trying to achieve.
Original List
$> set a {123.4:xyz 123.4:pqr 123.4:xyz 123.4:abc 123.4:mno}
$> puts $a
$> 123.4:xyz 123.4:pqr 123.4:xyz 123.4:abc 123.4:mno
I want my new list to contain following elements
$> puts $a
$> xyz pqr xyz abc mno
I tried split $a : but it did not work out for me. Please suggest what can be done.
set b [list]
foreach item $a {
catch {
regexp {\:(.*)} $item match tail
lappend b $tail
}
}
puts $b
It's possible to do above with split instead of regexp; I prefer regexp because you can extract arbitrary patterns this way.
If you've got Tcl 8.6:
set a [lmap x $x {regsub {^[^:]*:} $x ""}]
In 8.5, it's easier if you store in another variable:
set b {}
foreach x $a {
lappend b [regsub {^[^:]*:} $x ""]
}
In 8.4 and before, you also need a slightly different syntax for regsub:
set b {}
foreach x $a {
# Mandatory write back to a variable...
regsub {^[^:]*:} $x "" x
# Store the value; it isn't reflected back into the source list by [foreach]
lappend b $x
}
One Liner:
% set a {123.4:xyz 123.4:pqr 123.4:xyz 123.4:abc 123.4:mno}
% 123.4:xyz 123.4:pqr 123.4:xyz 123.4:abc 123.4:mno
set b [regexp -inline -all {[a-z]+} $a]
% xyz pqr xyz abc mno
Taddaaaa... No split required, if you have regex.
set a {123.4:xyz 123.4:pqr 123.4:xyz 123.4:abc 123.4:mno}
for {set i 0} {$i < [llength $a]} {incr i} {
lset a $i [lindex [split [lindex $a $i] ":"] 1]
}
puts $a
By making use of split command:
set a { 123.4:xyz 123.4:pqr 123.4:xyz 123.4:abc 123.4 :mno }
set c [list]
foreach ele $a {
lappend c [lindex [split $ele ":"] 1]
}
puts $c
output : xyz pqr xyz abc mno
set input {123.4:xyz 123.4:pqr 123.4:xyz 123.4:abc 123.4:mno}
set a ""
for {set i 0} {$i<[llength $input]} {incr i} {
set a "$a [string trim [lindex $input $i] {0,1,2,3,4,5,6,7,8,9,.,:}]"
}
This will trim the input.
If you have 8.5 and tcllib:
struct::list map $a {apply {{elem} {lindex [split $elem :] end}}}