Can you combine methods and capture the output? - methods

I have a method that looks like this:
proc getJobinfo {question} {
puts -nonewline "$question: "
flush stdout
gets stdin answer
# Can you combine totitle and trim into one line?
set titledanswer [string totitle $answer]
return $titledanswer
}
I would like to call trim and totitle in one line, is that possible?
For example, in Python:
company_name : str = userInput.trim().title()

yes , you can :
set titledanswer [string trim [string totitle $answer]]

Related

TCL script to find a script and add new text before searched string with number

Below is the partial content of my input file:
xyz
abc
MainContent(abc_dt) {
sc: it;
}
MainContent(xyz_cnt) {
sc : it;
}
MainContent(asd_zxc) {
sc : it;
}
Here, I want to search "MainContent" line and want to add new line before it ... this new line should have "Sb (text which is inside bracket in MainContent_1)"... this should also add opening bracket and closing bracket before next Sb occurance:
Expected output from the script:
xyz
abc
Sb(abc_dt_sb1) {
MainContent(abc_dt) {
sc: it;
}
}
Sb(xyz_cnt_sb2) {
MainContent(xyz_cnt) {
sc : it;
}
}
Sb(asd_zxc_sb3) {
MainContent(asd_zxc) {
sc : it;
}
}
Can someone please help to me create a TCL script for this?
This code will take your text to be processed on standard input and produce the results on standard output. Redirect it as required.
set counter 0
set expectBrace false
while {[gets stdin line] >= 0} {
if {!$expectBrace && [regexp {^\s*MainContent\s*\((\w+)\)} $line -> bits]} {
puts [format "Sb(%s_sb%d) \{" $bits [incr counter]]
set expectBrace true
}
puts $line
if {$expectBrace && [regexp {^\s*\}\s*$} $line]} {
puts "\}"
set expectBrace false
}
}
Using regular expressions to do the matching of the triggers for state changes in a little state machine (two states, governed by expectBrace) is pretty conventional parsing. I've used format to do the substitutions into the Sb(…) line; these are simple enough that you could use direct substitutions instead if you prefer.
I've not done anything about adding indentation.

Retrieving data from a particular column in a CSV File using TCL

I have a CSV File,containing two columns of data.
I want to retrieve two columns of the data in two separate lists.
I have tried the following code:
set fp [open "D:\\RWTH\\Mini thesis\\EclipseTCL\\TCL trial\\excelv1.csv" r]
set file_data [read $fp]
close $fp
set data [split $file_data " "]
puts $data
the output obtained is
{0,245
0.0025,249
0.005,250
0.0075,252
0.01,253
0.0125,255
0.015,256
.
.
.
}
The data is in 2 separate columns in the excel sheet. I wish to take the elements only from the 2nd column i,e
{245,
249,
250,
252,
253,
.
.
.
}
I would be glad, if someone can help me with this.
Using the file_data you have already read from the file, you can:
lmap row [split [string trim $file_data] \n] {
scan $row %*f,%d
}
That is, trim off white space before and after the data, split into rows, then from every row, scan one integer (skipping the real and the comma). All the scanned integers are collected in a list.
It is, however, a good idea to always use the right tool for the job.
package require csv
lmap row [split [string trim $file_data] \n] {
lindex [::csv::split $row] end
}
The ::csv::split command knows exactly how to split csv data. In this case, it isn't really necessary, but it's a good habit to use the csv package for csv data.
Documentation:
csv (package),
lindex,
lmap (for Tcl 8.5),
lmap,
package,
scan,
split,
string
You'd better to use "gets" to read each line from the file:
set fp [open "D:\\RWTH\\Mini thesis\\EclipseTCL\\TCL trial\\excelv1.csv" r]
set secondColumnData {}
while {[gets $fp line]>=0} {
if {[llength $line]>0} {
lappend secondColumnData [lindex [split $line ","] 1]
}
}
close $fp
puts $secondColumnData
gets
You may also try:
set data [read [open "D:\\RWTH\\Mini thesis\\EclipseTCL\\TCL trial\\excelv1.csv"]]
set result [regexp -all -inline -line -- {^.*,(.*)$} $data]
set items {}
foreach {tmp item} $result {
lappend items $item
}
puts $items
Output:
245 249 250 252 253 255 256

Ruby Regex for date and time

I am reading a log file and matching a pattern in the file; and if it matches do the regex on the line.
f = File.open(file,"r")
f.seek(0,IO::SEEK_END)
while true do
select([f])
line = f.gets
if line =~ pattern
a = line.sub(/ .*$/, "")
I have tried the above method and it fails for some reason.
So I have
"2017053007:00:02 : INFO : Processing bucket : East3"
coming into the line after matching the pattern. Just need to get this "2017053007:00:02" filtered out using the regex.
Thanks
input = "2017053007:00:02 : INFO : Processing bucket : East3"
input[/(?<=\A\d{10}:\d{2}:\d{2}\s:\s).*/]
#⇒ "INFO : Processing bucket : East3"
input[/\S+/]
#⇒ "2017053007:00:02"
Maybe you could try :
([0-9]+:[0-9]{2}:[0-9]{2})
without REGEX:
> a = "2017053007:00:02 : INFO : Processing bucket : East3"
> a.split.first
#=> "2017053007:00:02"

manipulating array: adding number of occurrences to the duplicate elements

(You are welcome to change the title to a more appropriate one!)
I got another Ruby/ERB question. I have this file:
ec2-23-22-59-32, mongoc, i-b8b44, instnum=1, Running
ec2-54-27-11-46, mongod, i-43f9f, instnum=2, Running
ec2-78-62-192-20, mongod, i-02fa4, instnum=3, Running
ec2-24-47-51-23, mongos, i-546c4, instnum=4, Running
ec2-72-95-64-22, mongos, i-5d634, instnum=5, Running
ec2-27-22-219-75, mongoc, i-02fa6, instnum=6, Running
And I can process the file to create an array like this:
irb(main):007:0> open(inFile).each { |ln| puts ln.split(',').map(&:strip)[0..1] }
ec2-23-22-59-32
mongoc
ec2-54-27-11-46
mongod
....
....
But what I really want is the occurrence number concatenated to the "mongo-type" so that it becomes:
ec2-23-22-59-32
mongoc1
ec2-54-27-11-46
mongod1
ec2-78-62-192-20
mongod2
ec2-24-47-51-23
mongos1
ec2-72-95-64-22
mongos2
ec2-27-22-219-75
mongoc2
The number of each mongo-type is not fixed and it changes over time. Any help with how can I do that? Thanks in advance. Cheers!!
Quick answer (maybe could be optimized):
data = 'ec2-23-22-59-32, mongoc, i-b8b44, instnum=1, Running
ec2-54-27-11-46, mongod, i-43f9f, instnum=2, Running
ec2-78-62-192-20, mongod, i-02fa4, instnum=3, Running
ec2-24-47-51-23, mongos, i-546c4, instnum=4, Running
ec2-72-95-64-22, mongos, i-5d634, instnum=5, Running
ec2-27-22-219-75, mongoc, i-02fa6, instnum=6, Running'
# a hash where we will save mongo types strings as keys
# and number of occurence as values
mtypes = {}
data.lines.each do |ln|
# get first and second element of given string to inst and mtype respectively
inst, mtype = ln.split(',').map(&:strip)[0..1]
# check if mtypes hash has a key that equ current mtype
# if yes -> add 1 to current number of occurence
# if not -> create new key and assign 1 as a value to it
# this is a if ? true : false -- ternary operator
mtypes[mtype] = mtypes.has_key?(mtype) ? mtypes[mtype] + 1 : 1
# combine an output string (everything in #{ } is a variables
# so #{mtype}#{mtypes[mtype]} means take current value of mtype and
# place after it current number of occurence stored into mtypes hash
p "#{inst} : #{mtype}#{mtypes[mtype]}"
end
Output:
# "ec2-23-22-59-32 : mongoc1"
# "ec2-54-27-11-46 : mongod1"
# "ec2-78-62-192-20 : mongod2"
# "ec2-24-47-51-23 : mongos1"
# "ec2-72-95-64-22 : mongos2"
# "ec2-27-22-219-75 : mongoc2"
Quite strightforward I think. If you don't understand something -- let me know.

Recovering hex data from a large log-file using Ruby and RegEx

I'm trying to filter/append lines of hex data from a large log-file, using Ruby and RegEx.
The lines of the log-file that I need look like this:
Data: 10 55 61 (+ lots more hex data)
I want to add all of the hex data, for further processing later. The regex /^\sData:(.+)/ should do the trick.
My Ruby-program looks like this:
puts "Start"
fileIn = File.read("inputfile.txt")
fileOut = File.new("outputfile.txt", "w+")
fileOut.puts "Start of regex data\n"
fileIn.each_line do
dataLine = fileIn.match(/^\sData:(.+)/).captures
fileOut.write dataLine
end
fileOut.puts "\nEOF"
fileOut.close
puts "End"
It works - sort of - but the lines in the output file are all the same, just repeating the result of the first regex match.
What am I doing wrong?
You are iterating over the same entire file. You need to iterate over the line.
fileIn.each_line do |line|
dataLine = line.match(/^\sData:(.+)/).captures
fileOut.write dataLine
end

Resources