Declare Variables In Groovy FOR Loop - groovy

I want to create a loop in Groovy, that declares two objects. Then sets these two objects to the JSON values produced from Terraform command.
for(int i = 0; i < 2; i++) {
def "instance_" + i
"instance_" + i = terraform output -json EC2Instance_IDs | jq -r '.[0]'
println instance_ + i
}
I've been getting errors, such as:
(instance_ + i) is a binary expression, but it should be a variable expression at line:
and,
unexpected token: instance_ # line 52, column 45. def instance_

You'll need something like this:
0.upto(1) { i ->
this."instance_$i" = "Fred $i"
println this."instance_$i"
}
Running the above snippet will give this output:
Fred 0
Fred 1
Or you could use a collection.

Related

How to print more than one value in the same line?

A simple example
Print("This is a number {0}", 1) // results in 4
How can I print 1 2 3 in the same line?
I have tried
Print(1, 2, 3) // Does not work
The forloop also does not work.
The while loop below prints the elements but each in a separate line since we do not have control over the line feed character \n:
fn Main() -> i32 {
var a: [i32;3] = (1, 2, 3); // Define array containing the numbers 1,2,3
var i:i32 =0;
while(i<3){
Print("{0}", a[i]);
i= i+1;
}
return 0;
}
which results in
1
2
3
here is the code
How can I get 1 2 3?
Short explanation: Providing more than 2 arguments to Print is currently not supported, so is not including a line break.
Details
This is definitely something that will change in the future, as the language is in its early design phases. You can find the source for Print as an instrinsic (non-carbon native) here: https://github.com/carbon-language/carbon-lang/blob/trunk/explorer/interpreter/type_checker.cpp#L2297
case IntrinsicExpression::Intrinsic::Print:
// TODO: Remove Print special casing once we have variadics or
// overloads. Here, that's the name Print instead of __intrinsic_print
// in errors.
if (args.size() < 1 || args.size() > 2) {
return CompilationError(e->source_loc())
<< "Print takes 1 or 2 arguments, received " << args.size();
}
CARBON_RETURN_IF_ERROR(ExpectExactType(
e->source_loc(), "Print argument 0", arena_->New<StringType>(),
&args[0]->static_type(), impl_scope));
if (args.size() >= 2) {
CARBON_RETURN_IF_ERROR(ExpectExactType(
e->source_loc(), "Print argument 1", arena_->New<IntType>(),
&args[1]->static_type(), impl_scope));
}
If you look at the code you will also see that the type of input variable is limited to just integer types. It would be easy enough create a PR to manually add support for 2-3 if you want :)

How to sort the characters of a word using awk?

I can't seem to find any way of sorting a word based on its characters in awk.
For example if the word is "hello" then its sorted equivalent is "ehllo". how to achieve this in awk ?
With GNU awk for PROCINFO[], "sorted_in" (see https://www.gnu.org/software/gawk/manual/gawk.html#Controlling-Scanning) and splitting with a null separator resulting in an array of chars:
$ echo 'hello' |
awk '
BEGIN { PROCINFO["sorted_in"]="#val_str_asc" }
{
split($1,chars,"")
word = ""
for (i in chars) {
word = word chars[i]
}
print word
}
'
ehllo
$ echo 'hello' | awk -v ordr='#val_str_asc' 'BEGIN{PROCINFO["sorted_in"]=ordr} {split($1,chars,""); word=""; for (i in chars) word=word chars[i]; print word}'
ehllo
$ echo 'hello' | awk -v ordr='#val_str_desc' 'BEGIN{PROCINFO["sorted_in"]=ordr} {split($1,chars,""); word=""; for (i in chars) word=word chars[i]; print word}'
ollhe
Another option is a Decorate-Sort-Undecorate with sed. Essentially, you use sed to break "hello" into one character per-line (decorating each character with a newline '\n') and pipe the result to sort. You then use sed to do the reverse (undecorate each line by removing the '\n') to join the lines back together.
printf "hello" | sed 's/\(.\)/\1\n/g' | sort | sed '{:a N;s/\n//;ta}'
ehllo
There are several approaches you can use, but this one is shell friendly, but the behavior requires GNU sed.
This would be more doable with gawk, which includes the asort function to sort an array:
awk 'BEGIN{FS=OFS=ORS=""}{split($0,a);asort(a);for(i in a)print a[i]}'<<<hello
This outputs:
ehllo
Demo: https://ideone.com/ylWQLJ
You need to write a function to sort letters in a word (see : https://www.gnu.org/software/gawk/manual/html_node/Join-Function.html):
function siw(word, result, arr, arrlen, arridx) {
split(word, arr, "")
arrlen = asort(arr)
for (arridx = 1; arridx <= arrlen; arridx++) {
result = result arr[arridx]
}
return result
}
And define a sort sub-function to compare two words (see : https://www.gnu.org/software/gawk/manual/html_node/Array-Sorting-Functions.html):
function compare_by_letters(i1, v1, i2, v2, left, right) {
left = siw(v1)
right = siw(v2)
if (left < right)
return -1
else if (left == right)
return 0
else
return 1
}
And use this function with awk sort function:
asort(array_test, array_test_result, "compare_by_letters")
Then, the sample program is:
function siw(word, result, arr, arrlen, arridx) {
result = hash_word[word]
if (result != "") {
return result
}
split(word, arr, "")
arrlen = asort(arr)
for (arridx = 1; arridx <= arrlen; arridx++) {
result = result arr[arridx]
}
hash_word[word] = result
return result
}
function compare_by_letters(i1, v1, i2, v2, left, right) {
left = siw(v1)
right = siw(v2)
if (left < right)
return -1
else if (left == right)
return 0
else
return 1
}
{
array_test[i++] = $0
}
END {
alen = asort(array_test, array_test_result, "compare_by_letters")
for (aind = 1; aind <= alen; aind++) {
print array_test_result[aind]
}
}
Executed like this:
echo -e "fail\nhello\nborn" | awk -f sort_letter.awk
Output:
fail
born
hello
Of course, if you have a big input, you could adapt siw function to memorize result for fastest compute:
function siw(word, result, arr, arrlen, arridx) {
result = hash_word[word]
if (result != "") {
return result
}
split(word, arr, "")
arrlen = asort(arr)
for (arridx = 1; arridx <= arrlen; arridx++) {
result = result arr[arridx]
}
hash_word[word] = result
return result
}
here's a very unorthodox method for a quick-n-dirty approach, if you really want to sort "hello" into "ehllo" :
mawk/mawk2/gawk 'BEGIN { FS="^$"
# to make it AaBbCc… etc; chr(65) = ascii "A"
for (x = 65; x < 91; x++) {
ref = sprintf("%s%c%c",ref, x, x+32)
} } /^[[:alpha:]]$/ { print } /[[:alpha:]][[:alpha:]]+/ {
# for gawk/nawk, feel free to change
# that to /[[:alpha:]]{2,}/
# the >= 2+ condition is to prevent wasting time
# sorting single letter words "A" and "I"
s=""; x=1; len=length(inp=$0);
while ( len && (x<53) ) {
if (inp~(ch = substr(ref,x++,1))) {
while ( sub(ch,"",inp) ) {
s = s ch;
len -= 1 ;
} } }
print s }'
I'm aware it's an extremely inefficient way of doing selection sort. The potential time-savings stem from instant loop ending the moment all letters are completed, instead of iterating all 52 letters everytime. The downside is that it doesn't pre-profile the input
(e.g. if u detect that this row is only lower-case, then u can speed it up with a lowercase only loop instead)
The upside is that it eliminates the need for custom-functions, eliminate any gawk dependencies, and also eliminate the need to split every row into an array (or every character into its own field)
i mean yes technically one can set FS to null string thus automatically becomes having NF as the string length. But at times it could be slow if input is a bit large. If you need unicode support, then a match()-based approach is more desirable.
added (x<53) condition to prevent run-away infinite loops in case input isn't pure ASCII letters

Groovy find the last iteration inside a cloure?

In groovy how to find the last iteration inside the closure.
def closure = { it->
//here I need to print last line only
}
new File (file).eachLine{ closure(it)}
Need to find inside the closure iteration.
Update 1:
Instead of reading a file, In Common How can i find the last iteration inside the closure ?
def closure = { it->
//Find last iteration here
}
I guess you need eachWithIndex:
def f = new File('TODO')
def lines = f.readLines().size()
def c = { l, i ->
if(i == lines - 1) {
println "last: $i $l"
}
}
f.eachWithIndex(c)
Of course in case of big files you need to count lines efficiently.

in tcl, how to convert a list arg into a numerical value

My code looks like this:
proc decimalToBin { decNum numBits } {
set one "1"
set zero "0"
set rd ""
for { set jj 0 } { $jj < $numBits } { incr jj } {
if { [expr {$decNum % 2}] > 0 } {
set rd "$one$rd"
} else {
set rd "$zero$rd"
}
set decNum [expr $decNum / 2]
}
return $rd
}
set List_Of_Values_To_Sweep {0 1 63 255}
foreach i {$List_Of_Values_To_Sweep} {
set temp [decimalToBin $i 8]
}
I get an error that looks like this:
ncsim: *E,TCLERR: can't read "List_Of_Values_To_Sweep": no such
variable.
while executing "expr $decNum % 2"
How can I convert a value in the list so that it plays nicely with the % operand?
Remove the braces around $List_Of_Values_To_Sweep. They prevent variable substitution, so the string "$List_Of_Values_To_Sweep" (with a literal dollar sign) is assigned to i and used in the loop, which means that the decnum argument to decimalToBin will also be this string instead of the numeric value you intended. Double evaluation inside the condition means that the code tries to perform variable substitution on the string, but as there is no variable named List_Of_Values_To_Sweep inside decimalToBin, it fails with the error message you quoted.
Also, the if condition is evaluated as if passed to expr, so you don't need to call expr inside it:
if { $decNum % 2 > 0 } {
Then again, you don't even need it. You can replace the whole if construct with
set rd [expr {$decNum % 2}]$rd
since that expr invocation will give you the one or zero that you want.

Iterate and print content of groovy closures

In a loop I create 4 closures and add them to a list:
closureList = []
for (int i=0; i<4; i++) {
def cl = {
def A=i;
}
closureList.add(cl)
}
closureList.each() {print it.call()println "";};
This results in the following output:
4
4
4
4
But I would have expected 0,1,2,3 instead. Why does the 4 closures have the same value for A?
Yeah, this catches people out, the free variable i is getting bound to the last value in the for loop, not the value at the time the closure was created.
You can either, change the loop into a closure based call:
closureList = (0..<4).collect { i ->
{ ->
def a = i
}
}
closureList.each { println it() }
Or create an extra variable that gets re-set every time round the loop, and use that:
closureList = []
for( i in (0..<4) ) {
int j = i
closureList << { ->
def a = j
}
}
closureList.each { println it() }
In both of these variants the variable closed by the closure is created afresh each time round the loop, so you get the result you'd expect

Resources