How to use right_pad function with undefined values? - freemarker

I have a list of elements in my FTL Script, now I need to use the function right_pad with these elements. This works with elements which exists but if an element does not exist, the right_pad function does nothing.
Example
[${(listofElements["fieldThatExists"]?right_pad(5))!""}]
Assuming the value of the field is 123 this will output
[123 ]
This field does not exist:
[${(listofElements["notExistingField"]?right_pad(5))!""}]
Then the output is
[]
But I expected this output
[ ]
How can I retrieve the expected value if the field is undefined. I know I can put
!" "
instead of
!""
but this not enough because the value of right pad is not constant (not always 5).

Put right_pad outside:
[${(listOfElements["fieldThatExists"]!"")?right_pad(5)}]
But since the right-hand operand of ! is optional, there's shorter version:
[${listOfElements["fieldThatExists"]!?right_pad(5)}]

Related

how to compare array element of one array with elements in another array in bash?

I have stored some strings in array1, lets say
array1[0]=apple
array2[1]=orange
and array2 contains
array2[0]=apple
array2[1]=mango
I want to loop through each element and check if they match. I tried using this condition inside loop but it didnot work
if [ "$array[i]" = "$array2[j]" ]
To access the elements of an array in bash you have to use ${array[i]} instead of just $array[i]. Because the [ cannot normally be part of a variable name, bash interprets $array[i] as ${array} followed by a literal [i].
By the way: https://www.shellcheck.net/ would have found this error.

What does the # symbol mean in this bash for loop? [duplicate]

I know that one can get the length of an array in bash by doing ${#arrayname[#]}.
My question is: is this just something that I have to memorize, or can this syntax be broken down into understandable parts? For instance, what does the # symbol mean where one would expect to find the index? Why the #?
# at the beginning of a variable reference means to get the length of the variable's value. For a normal variable this means its length in characters. # is the "number" sign, so you can remember this as meaning "the number of things in the variable".
# or * in an array index means to use the whole array, not a specific element, and instead of returning the number of characters, it returns the number of array elements. * is used as a wildcard in many contexts, so this should be easy to remember. Also, $* and $# are used to mean all the arguments to a shell script, so the parallel with all the array elements should be obvious.
You can't just write ${#arrayname} because when you use an array variable without a subscript, it's equivalent to element 0 of the array. So ${#arrayname} is the same as ${#arrayname[0]}, which is the number of characters in the first element of the array.
You should memorize. :) The # usually means number. e.g. the
$# - is the number of arguments
${#str} - length of the string $str
${#arr[#]}" - length (number of elements) of the array arr
${#arr} - the length of the 1st element of the array (like the str above)
Unfortunately the ${parameter#word} or ${parameter##word} has nothing with numbers. (it removes the shortest/longest word from the beginning of the parameter.
And also, the # .... is comment ;)
In general usage of form ${#PARAMETER} returns the length in number of characters and NOT bytes of the parameter's value.
myString="Hello StackOverflow!"
printf "%s\n" "${#myString}"
20
But for arrays, this expansion type has two meanings:
For individual elements, it reports the string length of the element
(as for every "normal" parameter)
For the mass subscripts # and * it
reports the number of set elements in the array
Consider an example over arrays,
myArray=(1 2 3 4 15)
printf "%s\n" "${myArray[#]}" # <-- Gives me list of elements
1
2
3
4
15
printf "%s\n" "${#myArray[#]}" # <-- Gives me number of elements
5
It gets interesting now, the length of the last element 2 can be obtained by doing
printf "%s\n" "${#myArray[4]}"
2
The '#' acts the same way as '*'. Instead of providing a specific index this references the full thing.
The '#' is telling bash you want the length
https://www.cyberciti.biz/faq/finding-bash-shell-array-length-elements/

In TI-BASIC, how do I display the Variable Name given only the variable?

I'm creating a function that displays a lot of variables with the format Variable + Variable Name.
Define LibPub out(list)=
Func
Local X
for x,1,dim(list)
list[x]->name // How can I get the variable name here?
Disp name+list[x]
EndFor
Return 1
EndFunc
Given a list value, there is no way to find its name.
Consider this example:
a:={1,2,3,4}
b:=a ; this stores {1,2,3,4} in b
out(b)
Line 1: First the value {1,2,3,4} is created. Then an variable with name a is created and its value is set to {1,2,3,4}.
Line 2: The expression a is evaluated; the result is {1,2,3,4}. A new variable with the name b is created and its value is set to `{1,2,3,4}.
Line 3: The expression b is evaluated. The variable reference looks up what value is stored in b. The result is {1,2,3,4}. This value is then passed to the function out.
The function out receives the value {1,2,3,4}. Given the value, there is no way of knowing whether the value happened to be stored in a variable. Here the value is stored in both a and b.
However we can also look at out({1,1,1,1}+{0,2,3,4}).
The system will evaluate {1,1,1,1}+{0,2,3,4} and get {1,2,3,4}. Then out is called. The value out received the result of an expression, but an equivalent value happens to be stored in a and b. This means that the values doesn't have a name.
In general: Variables have a name and a value. Values don't have names.
If you need to print a name, then look into strings.
This will be memory intensive, but you could keep a string of variable names, and separate each name by some number of characters and get a substring based on the index of the variable in the list that you want to get. For instance, say you want to access index zero, then you take a substring starting at (index of variable * length of variable name, indexofvariable *length + length+1).
The string will be like this: say you had the variables foo, bas, random, impetus
the string will be stored like so: "foo bas random impetus "

Removing the subscript/Index of an array in awk

I am using the awk's concept of storing the values as a subscript/Index of an array. Please have a look at the code below
stringVariable="hi,bye,cool.hot,how,see";
split(stringVariable,stringArray,",");
#This loop will iterate and stores the RIDs in the requestIds variable into an array
for(tr=1;tr<=length(stringArray);tr++)
{
Count++;
referenceIdArray[stringArray[tr]]++;
}
So in my referenceId array I will be having hi,bye,cool,hot,how,see
let me consider a sample file which has the following values
hi
bye
gone
My aim is to get the values from the file and to match with the array declared previously and if any of the values matches print the value from a file
awk script
awk '{BEGIN (Array loading done previously)} {if($0 in referenceIdArray) {print $0}}'
So this will give me the desired result. But assuming that the "hi" will appear only once in an array and hence when the action block finds the value, the value should be printed and also the corresponding entry in the array which is referenceIdArray["hi"] should also be removed in order to make the search effecient. Since they are stored as subscript I am not sure how to remove the entry. Any suggesions regarding this. Thank you.
You can remove an individual element of an array using the delete statement:
delete array[index]
ref: http://www.math.utah.edu/docs/info/gawk_12.html

TCL array values updation based on command line argument

I am trying to substitute variable value inside array so as to update array values based on command line inputs.
e.g. I am receiving IP address as command line argument for my TCL script and trying to update commands with recvd IP value.
My array is:
array set myArr { 1 myCmd1("192.268.2.1","abc.txt")
2 myCmd2("192.268.2.1","xyz.txt")
3 myCmd3("192.268.2.1","klm.txt")
}
Here, "192.268.2.1" will actually be supplied as command line argument.
I tried doing
array set myArr { 1 myCmd1($myIP,"abc.txt")
2 myCmd2($myIP,"xyz.txt")
3 myCmd3($myIP,"klm.txt")
}
and other combinations like ${myIP}, {[set $myIP]} but none is working.
Thanks in advance for any help/inputs.
Use the list command
% set myIP 0.0.0.0
0.0.0.0
% array set myArr [list 1 myCmd1($myIP,"abc.txt") 2 myCmd2($myIP,"xyz.txt") 3 myCmd3($myIP,"klm.txt")]
% puts $myArr(1)
myCmd1(0.0.0.0,"abc.txt")
% puts $myArr(3)
myCmd3(0.0.0.0,"klm.txt")
%
I think your code will be easier to understand and easier to maintain if you don't try to use array set in this instance. You can get away with it if you are careful (see answers related to using list) but there's really no reason to do it that way when a more readable solution exists.
Here's my solution:
set myArr(1) "myCmd1($myIP,\"abc.txt\")"
set myArr(2) "myCmd2($myIP,\"xyz.txt\")"
set myArr(3) "myCmd3($myIP,\"klm.txt\")"
try:
array set myArr [list myCmd1($myIP, "abc.txt") 2 myCmd2($myIP, "xyz.txt") ... ]
Why? Because when you write {$var} in Tcl, it means $var and not the contents of the variable var. If you use list to create the list instead of the braces, variables are still evaluated.

Resources