I'm trying to test if a variable has a value with a simple if in puppet, but it never reachs the else condition that I'm trying to achieve.
ex :
if $::some_var['some:name'] != undef {
$var = $::some_var['some:name']
}
else {
$var = $::some_var['another:name']
}
some_var cames from a json file, and if the ::some_var['some:name'] is emtpy it will put $var as empty, but what I'm trying to achieve is, if the var is defined but is empty to actually go through the else, can this be achieved using for instance
if defined($::some_var['some:name']) {
$var = $::some_var['some:name']
}
else {
$var = $::some_var['another:name']
}
I resolved it by using
$var = ''
if $var =~ String[1] {
notify{"The value is: ${var}": }
}
else {
notify{"The value is: empty": }
}
file {'/tmp/helloworld.txt': ensure => present, content => "Hello World!", }
If I define $var whit some value, I'll met the if condition, if $var is empty I'll met the else.
Related
Thanks for any help.
I am currently trying to get a DXL script to return all occurrences for a certain word in the module in the outlink. The outlink "test" will need to be a wild card, if possible, to find all other outlnks that have the word "test" unsure on if that's possible. The attribute in the outlink will be "Compliance". Maybe need a while loop?
Link outlnk
string tmn = ""
string Status = ""
int Count = 0
for outlnk in obj-> "*" do
{
tmn = fullName source(outlnk)
Object tgt = source(outlnk)
if(matches("Test", tmn))
{
Status = tgt."Compliance"
if (Status == "Compliant"){
Count++
}
}
}
display Count""
I tried to get the above code to work with the below while loop. But can not figure out the correct syntax.
Regexp Check = regexp2 "Compliant"
while (!null Status && Check Status)
{ Status = Status[end 0 + 1:]
Count++
}
I have the following PS command which is as follows:
if(get-customcmdlet) {
$var = var1
} else {
$var = var2
}
Here get-customcmdlet is throwing an exception.
So, I have modified the above statement as follows:
try {
get-customcmdlet
$var = var1
}
catch {
$var = var2
}
Please let me know if it is correct way to handle the exception generated from get-customcmdlet
You have to understand that try-catch does something different than your earlier if-else.
Try-Catch checks if a execption is thrown, while if(get-customcmdlet) checks if your function returns something unequal $false or $null.
Given that your function supplies something even when an exception occurs on one point, you can just combine both toghether like this:
try {
if(get-customcmdlet) {
$var = var1
}
else {
$var = var2
}
}
catch {
$var = var2
}
This way $var equals var2 when the function returns an exception and when it returns nothing or $false.
How do I exit a promise from within a promise? The perl6 docs do not provide an easy method. For example:
my $x = start {
loop { # loop forever until "quit" is seen
my $y = prompt("Say something: ");
if $y ~~ / quit / {
# I want to exit the promise from here;
# "break" and "this.break" are not defined;
# "return" does not break the promise;
# I do NOT want an error exception when exiting a promise;
# I want to return a value as the result of this promise;
}
else { say $y; }
}
}
I do not want to be in the promise loop forever. break() and this.break() are not recognized, and return does not break the promise.
Use the last keyword to quit the loop.
The kept value of a start block is the value returned by its last statement.
So:
my $x = start {
loop { # loop forever until "quit" is seen
my $y = prompt("Say something: ");
if $y ~~ / quit / {
last
}
else { say $y; }
}
42 # <-- The promise will be kept with value `42`
}
I want to explode coma from an array value.
My code is.
$to_ids_string = "";
$to_id = $this->input->post('to');
for ($r = 0; $r < count($this->input->post('to')); $r++) {
if ($to_ids_string != "") {
$to_ids_string = $to_ids_string . "," . $to_id[$r];
} else {
$to_ids_string = $to_id[$r];
}
}
echo $to_ids_string;
$a = explode(',', $to_ids_string);
foreach ($a as $item) {
echo("<li>$item</li>");
exit;
}
when i echo $to_ids_string it will return 2,3 but when i loop in foreach it only return 2 not show 3.
Because of your exit, if you use exit like that, then it is the end of your program and it doesn't echo anymore.
You forget to remove exit; from foreach loop. When you write exit, execution of your code stops. Hence you are not getting desired output.
Happens due to exit.
Please remove exit from your code.
how to detect parameter is not empty on laravel?
example i have params body like below :
{
email : "haha#example.com"
}
when params email is exist's i want echo some string, example : "parameter email is exist's"
if params email not exits just echo "parameter is not exist's"
if(params['email'] != null) {
print params email is exists's
}else{
print params email doesn't exists's
}
how do that, i'm new on php / laravel?
Try Below Code
if( isset(params['email']) && !empty(params['email']) ) {
echo " params email exists"
}else{
echo "params email doesn't exist"
}
Object oriented Laravel way is:
if($email = array_get($params, 'email')){
echo " params email exists";
}else{
echo "params email doesn't exist";
}
Use php build in empty() function which checks if array key exists, is not null and is not empty according to data type.
if (!empty($params['email']) {
// not empty
}
From manual:
Returns FALSE if var exists and has a non-empty, non-zero value. Otherwise returns TRUE.
The following things are considered to be empty:
"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
$var; (a variable declared, but without a value)
If you want to know if the array key exists use
if(array_key_exists('email',$params)){
//true
}
to check if the key exists and its not empty use
if(array_key_exists('email',$params)&&!empty($params['email']){
//true
}
see Robert's answer for what is seen as empty by the empty function.