Comma separated coordinates printing in Tcl using puts - string

How to print a triangle coordinates (1,2) (3,4) (5,6) using puts? I am getting errors for the quotes.
puts "triangle just added is" "( " $ax "," $ay ") " "( " $bx "," $by ") " "( " $cx "," $cy ") "

puts takes one argument (A string to write to standard output) or two (A channel to write to and the string). Well, and the optional -nonewline option, so really 2 or 3 arguments. You're giving it a lot more than that, hence errors.
Like many scripting languages, tcl will expand variables inside double-quoted strings:
puts "triangle just added is ($ax,$ay) ($bx,$by) ($cx,$cy)"

Related

Remove spaces from a string but not new lines in lua

I used string.gsub(str, "%s+") to remove spaces from a string but not remove new lines, example:
str = "string with\nnew line"
string.gsub(str, "%s+")
print(str)
and I'm expecting the output to be like:
stringwith
newline
what pattern should I use to get that result.
It seems you want to match any whitespace matched with %s but exclude a newline char from the pattern.
You can use a reverse %S pattern (that matches any non-whitespace char) in a negated character set, [^...], and add a \n there:
local str = "string with\nnew line"
str = string.gsub(str, "[^%S\n]+", "")
print(str)
See an online Lua demo yielding
stringwith
newline
"%s" matches any whitespace character. if you want to match a space use " ". If you want to define a specific number of spaces either explicitly write them down " " or use string.rep(" ", 5)

Is there a Go function that works like linux cut?

This is probably a very basic question, but I have not been able to find an answer after reviewing the strings package docs.
Basically, all I want to do is the equivalent of:
echo "hello world" | cut -d" " -f2
echo "hello world" | cut -d" " -f2
This splits the string "hello world" using spaces as delimeters, and selects only the 2nd part (1-indexed).
In Go for spitting there is strings.Split() which returns a slice, which you can index or slice however you like.
s := "hello world"
fmt.Println(strings.Split(s, " ")[1])
This outputs the same. Try it on the Go Playground. If the input is not guaranteed to have 2 parts, the above indexing ([1]) might panic. Check the length of the slice before doing so.
There is the strings.Split() function which splits the string at the specified sub-string.
There are also the functions Fields(s string) []string, and FieldsFunc(s string, f func(rune) bool) []string.
The former split the string at spaces, and the later uses the given function to determine if the string must be split.
The difference between Split and Fields is that Fields consider multiple consecutive spaces as one split location. strings.Fields(" foo bar baz ")) yields ["foo" "bar" "baz"], and strings.Split(" foo bar baz ", " ") yields ["" "" "foo" "bar" "" "baz" "" "" ""].

Confused on the function of "r"(raw string characters)

Confused over the purpose of "r". As i understand it helps to read as a normal character than its usage as an escape character
I tried multiple codes as follows and all are giving the same output. This is making me confused on the real interpretation of "r". While i agree with first 3 lines of code.Fourth one is where im confused.
1.re.sub("n\'t", " not", " i am n't happy")
2.re.sub("n\'t", " not", " i am n\'t happy")
3.re.sub(r"n\'t", " not", " i am n\'t happy")
4.re.sub(r"n\'t", " not", " i am n't happy")
Result of all 4 above is :'
' i am not happy'
import re
re.sub(r"n\'t", " not", " i am n't happy")
Given that i have used "r" i expected the backslash to be treated as a characters and not escape character
Actual Output
' i am not happy'
Expected Output
' i am n't happy'
The thing is that there are two layers of -escaping: in the string literal, and in the regex. And in neither does \' have a special meaning, and it's just treated as '.
What using r"" does here is to skip the first string-literal escaping, so that a literal \ is included in the string, but then the regex sees the string \' and just treats it as '.
So all four come down to replacing n't with not.
You still need double backslashes to match a literal backslash.

How to output (using the write statement) an apostrophe " ' "?

I want to use system command in Fortran 90 for executing folowing command:
command = awk '{print "C"NR,$1,$2,$3}' filename1 > filename2
call system(trim(command))
here my filename1 and filename2 are variables in a Fortran 90 program.
But the problem is that any character can be assigned to a variable which are enclosed between apostrophes and my variable should also be consisting of apostrophes. I don't know how to type it in Fortran 90.
Just use two apostrophes in a row inside the string
command = 'awk ''{print "C"NR,$1,$2,$3}'' filename1 > filename2'
Additionally, because I did not notice filename1 and filename2 are variables, you must append them as chw21 shows:
// trim(filename1)//' > '//trim(filename2)
You can try to use a parameter for single quotes, like this:
character, parameter :: sq = "'"
Then you can chain things together like this:
command = 'awk '//sq//'{print "C"NR,$1,$2,$3}'//sq//' '// &
trim(filename1)//' > '//trim(filename2)
Or, you can swap between single- and double quoted strings:
command = "awk '" // '{print "C"NR,$1,$2,$3}' // "' " // &
trim(filename1) // ' > ' // trim(filename2)
What you shouldn't do at all is using the Hollerith format instruction:
write(command, 100) trim(f1), trim(f2)
100 FORMAT(29Hawk '{print "C"NR,$1,$2,$3}' , A, " > ", A)
That's why I'm not even telling you. Oh.

R String Interpretation: why does "\040" get interpreted as " " and what other potential pitfalls could I come across in string interpretation?

I was helping someone today regex some info out of a pdf file that we read in as a txt file. Unfortunately the tm packages readPDF function was not working correctly at the time, though through a few modifications we were able to get it to work just fine. While we were regexing out some of the fluff from the .txt file we found something that was surprising to most of us, namely that the string "\040" gets interpreted as a space, " ".
> x <- "\040"
> x
> [1] " "
This doesn't happen for other, similar character strings (i.e. "\n" or "\t") that you may expect this to happen for.
> y <- "\n"
> y
> [1] "\n"
> z <- "\t"
> z
>[1] "\t"
Why is this? What other character strings are interpreted differently in R?
EDIT:
It seems after simple experimentation, any "\xxx" where x are digits yields a different result. What is the value of this?
Take a look here: http://stat.ethz.ch/R-manual/R-devel/library/base/html/Quotes.html
Backslash is used to start an escape sequence inside character constants. Escaping a character not in the following table is an error.
...
\nnn character with given octal code (1, 2 or 3 digits)
Then take a look at this ASCII table to see how octal codes get represented. As you will see 040 is a space.
And just for fun:
> '\110\145\154\154\157\040\127\157\162\154\144\041'
[1] "Hello World!"

Resources