Create a string with a variable interpolated, assign a value to that variable afterwards and have the string print with the assigned value? - coffeescript

What I'm hoping for is something to this effect:
str = "This is a #{testing}"
testing = "test"
console.log(str)
# => "This is a test"
The use case doesn't make switching the order of the variable definitions efficient, so I would like to avoid that if possible.

Related

How to check whether hash has a value for the key in puppet

I have a hash defined as below:
Hash[String, String] $hashtest = { "abc" => "test1", "xyz" => "test2" },
I have String variable, I need to search for the given key in the hash and if a value is found, I need to assign that value to the variable "result" otherwise I need to assign a default value "test". How can I do this is in puppet? Or only way to do this is using if else condition?
It should be similar like this, but the below code is not working. Kindly correct me what I'm doing wrong.
String $variable = $hashtest[$key] ? { true => $hashtest[$key], false => "test" },
It would be really helpful if someone helps me with this thanks in advance.
I am assuming in your pseudocode you are intending to assign a value with a return from a selector, and not also providing a pseudocode for a ternary-like expression in Puppet. With that in mind, we can achieve this with something similar to Python:
String $variable = $key in $hashtest ? {
true => $hashtest[$key]
false => "test"
}
Note that prior to Puppet 4 you would need the has_key? function (analogous to has_key Hash method in Ruby) from stdlib:
String $variable = has_key($hashtest, $key) ? {
true => $hashtest[$key]
false => 'test'
}
In stdlib there is also a function roughly equivalent to a "null coalescing" operator in other languages (null being roughly equivalent to undef type in Puppet and nil in Ruby) that would provide a cleaner expression:
String $variable = pick($hashtest[$key], 'test')
Similar to the coalescing patterns in other languages, pick will return the first argument that is not undef or empty.
As well as matts answer you can also use the following
$variable = $hashtest[$key].lest || { 'test' }
$variable = ($key in $hashtest).bool2str($hashtest[$key], 'test')
$variable = $hashtest.has_key($key).bool2str($hashtest[$key], 'test')
All of these options are missing the most simple and powerful option that's been available from the core library since puppet 6; the get function.
The get function allows you specify a dot separated path of nested keys to look up as the first argument, and a default value for the second. By default, it will return undef if a value cannot be found, making it ideal for use in conditional expressions since undef is the only value in puppet that automatically converts to false. You can even pass a lambda to it to handle missing values.
In your case, the answer is as simple as $variable = $hashtest.get($key, 'test') or $variable = get($hashtest, $key, 'test'), though I personally find the first option easier to read.

Dart: How to force string interpolation on a variable

I have a variable that contains a string with interpolated variables. In the code below, that variable is template. When I pass this variable to generateString function, I want to apply string interpolation on it because the values which interpolated variables require are available in generateString function only.
void main() {
String template = '<p>\${name}</p>';
var res = generateString(template);
}
generateString(template) {
var name = 'abc';
print(template);
return template;
}
The problem is when I am printing and returning template inside generateString fn, I am getting <p>${name}</p> instead of <p>abc</p>. Is there a way to explicitly tell the dart to so string interpolation?
I am new to Dart. I don't know if it is even possible to achieve or not. Please suggest how do I do this.
Edit: Based on the inputs from other users, I would like to make a clarification about the scenario presented. The value of template variable is not a string literal. I get that from UI as a user input. I have shown it here as a string literal for code simplicity. Also, please consider that name and template are not in the same scope in my scenario.
The other answers so far are wrong.
String interpolation (looking for $, etc) happens only while compiling from the source code to the value in memory. If that string in turn also has a $, it's no longer special.
It's not possible to trigger interpolation past the original compilation step. You can write a templating system that would look for something like {{name}} in the value, and replace it with the current value of name.
If you have the template and the variable in the same scope, it works as expected.
// evaluate variable inside ${}
var sport = 'basketball';
String template = 'I like <p>${sport}</p>';
print(template);
I didn't fully understand your question maybe this will help
void main() {
print(generateString('abc')); //<p>abc</p>
}
generateString(String template) {
return r"<p>" "$template" r"</p>";
}
Walter White here.
You must define the variable name as global var, so it can "cook" the string for you

Can I have a string containing a delegate that is evaluated at runtime?

Can I have a string that contains a delegate that gets expanded at various times during runtime?
$pattern = "(?m)^INFO\:(?:\s|\t)*$({script:$marker})\:(?:\s|\t)*(?<url>.*)$"
$marker = "Some marker value"
:
#Do something with the resulting pattern containing the marker value
:
$marker = "Some other marker value"
:
#Do something with the pattern having the new marker value
and so on... I'd prefer not to have to keep redefining the string... or having a function that builds it. It seems so much more succinct if I could just have a few characters in the string that get evaluated when the string is needed vs. when the $pattern value is set.
you can do
$pattern = {"(?m)^INFO\:(?:\s|\t)*($script:marker)\:(?:\s|\t)*(?<url>.*)$"}
and then later use
$pattern.invoke()
(Assuming you want $script:marker to be the characters that get set later, your original example has $({script:$marker}), but that won't work if it is supposed to do what I think it should ;))
In general: Define the term as Scriptblock using {} and later .invoke() to evaluate it.
Just make sure there is no confusion about the types within the curly brackets, otherwise you might get some strange results...

Concatenate characters for the name of a struct in Matlab

I want program a struct in Matlab for saving some parameters.
The struct's name has to change every iteration in a loop, thus in each iteration I make a new struct. Therefore I want something like this:
index={'01','02','03'};
letter={'aa','bb','cc'};
names={'Peter','John','Michael'};
for(i=1:numel(index)){
......
strcat(str, index{i}, letter{i})(i).name = names{i};
}
Then, when the loop has finished I have 3 structs with the next names:
- str01aa{
name = 'Peter'
}
- str02bb{
name = 'John'
}
- str03cc{
name = 'Michael'
}
My problem is that the strcat function with the bracket (i) is not good defined, and the structs are not created.
I hope you can help me.
Thanks.
strcat(str, index{i}, letter{i})(i).name isn't a valid operation, because strcat returns a sting object, which can't possess fields. You need to make that string into a variable name using genvarname (documentation), like so:
index={'01','02','03'};
letter={'aa','bb','cc'};
names={'Peter','John','Michael'};
for(i = 1:numel(index))
{
......
genvarname(strcat('str', index{i}, letter{i}))(i).name = names{i};
}
Note that I changed str to 'str' for consistency with your example. As a general rule, dynamically constructed variable names are bad practice because they make debugging a nightmare.
Let me make a suggestion; instead of having a bunch of structs with different, seemingly arbitrary names, why not try something like this:
index={'01','02','03'};
letter={'aa','bb','cc'};
names={'Peter','John','Michael'};
for(i = 1:numel(index))
{
......
yourStruct(i).id = strcat('str', index{i}, letter{i});
yourStruct(i).name = names{i};
}
Either way, good luck!

Perl If statement without parameters just plain variable is the EXP

I just wanted to know what does this code do?
my $string_1 = "foo bar";
my $val = 3;
if($string_1) {
}
basically what happen if you just use a variable inside an if statement?
thanks
It checks if the value of the variable is true. In Perl, everything is true but the following:
0 and the string '0'
undef
() (the empty list)
'' (an empty string)
This is documented in perlsyn. It also works with any other kind of value. You can also put a string, a function call inside the if condition. The behavior is always the same.
this will check variable '$string' has not null value