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.
i am having a problem with my code
alphabet [a-zA-Z]
var {alphabet}+[{chiffre}{alphabet}_]*
{varIntDecl} {
for(int i=5 ;i<yyleng;i++)tab[cmp].token[i-5]=yytext[i]; strcpy(tab[cmp].value,"0");strcpy(tab[cmp].type,"Var");
tab[cmp].indiceTS= cmp;
strcpy(tab[cmp++].typeValeur,"int");
yylval.indiceTS=cmp;
return (INTVARIABLEDECL);
}
{var} {
for(int i=0;i<cmp;i++){
if(!strcmp(tab[i].token,yytext)) {
return (VARINT);
}
}
printf("Error Variable non trouvé Please Declare First ");
}
varIntDecl {int}{blancs}{var};
when i'm executing this
i have a yacc part but it's irrelevant to this
i'm getting this conflict of var executing everytime even tho it's not what i wanted to do
here is the result of execution:
int abc
Error Variable non trouvé Please Declare First
int adc
Error Variable non trouvé Please Declare First
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.
The most egregiously redundant code construct I often see involves using the code sequence
if (condition)
return true;
else
return false;
instead of simply writing
return (condition);
I've seen this beginner error in all sorts of languages: from Pascal and C to PHP and Java. What other such constructs would you flag in a code review?
if (foo == true)
{
do stuff
}
I keep telling the developer that does that that it should be
if ((foo == true) == true)
{
do stuff
}
but he hasn't gotten the hint yet.
if (condition == true)
{
...
}
instead of
if (condition)
{
...
}
Edit:
or even worse and turning around the conditional test:
if (condition == false)
{
...
}
which is easily read as
if (condition) then ...
Using comments instead of source control:
-Commenting out or renaming functions instead of deleting them and trusting that source control can get them back for you if needed.
-Adding comments like "RWF Change" instead of just making the change and letting source control assign the blame.
Somewhere I’ve spotted this thing, which I find to be the pinnacle of boolean redundancy:
return (test == 1)? ((test == 0) ? 0 : 1) : ((test == 0) ? 0 : 1);
:-)
Redundant code is not in itself an error. But if you're really trying to save every character
return (condition);
is redundant too. You can write:
return condition;
Declaring separately from assignment in languages other than C:
int foo;
foo = GetFoo();
Returning uselessly at the end:
// stuff
return;
}
I once had a guy who repeatedly did this:
bool a;
bool b;
...
if (a == true)
b = true;
else
b = false;
void myfunction() {
if(condition) {
// Do some stuff
if(othercond) {
// Do more stuff
}
}
}
instead of
void myfunction() {
if(!condition)
return;
// Do some stuff
if(!othercond)
return;
// Do more stuff
}
Using .tostring on a string
Putting an exit statement as first statement in a function to disable the execution of that function, instead of one of the following options:
Completely removing the function
Commenting the function body
Keeping the function but deleting all the code
Using the exit as first statement makes it very hard to spot, you can easily read over it.
Fear of null (this also can lead to serious problems):
if (name != null)
person.Name = name;
Redundant if's (not using else):
if (!IsPostback)
{
// do something
}
if (IsPostback)
{
// do something else
}
Redundant checks (Split never returns null):
string[] words = sentence.Split(' ');
if (words != null)
More on checks (the second check is redundant if you are going to loop)
if (myArray != null && myArray.Length > 0)
foreach (string s in myArray)
And my favorite for ASP.NET: Scattered DataBinds all over the code in order to make the page render.
Copy paste redundancy:
if (x > 0)
{
// a lot of code to calculate z
y = x + z;
}
else
{
// a lot of code to calculate z
y = x - z;
}
instead of
if (x > 0)
y = x + CalcZ(x);
else
y = x - CalcZ(x);
or even better (or more obfuscated)
y = x + (x > 0 ? 1 : -1) * CalcZ(x)
Allocating elements on the heap instead of the stack.
{
char buff = malloc(1024);
/* ... */
free(buff);
}
instead of
{
char buff[1024];
/* ... */
}
or
{
struct foo *x = (struct foo *)malloc(sizeof(struct foo));
x->a = ...;
bar(x);
free(x);
}
instead of
{
struct foo x;
x.a = ...;
bar(&x);
}
The most common redundant code construct I see is code that is never called from anywhere in the program.
The other is design patterns used where there is no point in using them. For example, writing "new BobFactory().createBob()" everywhere, instead of just writing "new Bob()".
Deleting unused and unnecessary code can massively improve the quality of the system and the team's ability to maintain it. The benefits are often startling to teams who have never considered deleting unnecessary code from their system. I once performed a code review by sitting with a team and deleting over half the code in their project without changing the functionality of their system. I thought they'd be offended but they frequently asked me back for design advice and feedback after that.
I often run into the following:
function foo() {
if ( something ) {
return;
} else {
do_something();
}
}
But it doesn't help telling them that the else is useless here. It has to be either
function foo() {
if ( something ) {
return;
}
do_something();
}
or - depending on the length of checks that are done before do_something():
function foo() {
if ( !something ) {
do_something();
}
}
From nightmarish code reviews.....
char s[100];
followed by
memset(s,0,100);
followed by
s[strlen(s)] = 0;
with lots of nasty
if (strcmp(s, "1") == 0)
littered about the code.
Using an array when you want set behavior. You need to check everything to make sure its not in the array before you insert it, which makes your code longer and slower.
Redundant .ToString() invocations:
const int foo = 5;
Console.WriteLine("Number of Items: " + foo.ToString());
Unnecessary string formatting:
const int foo = 5;
Console.WriteLine("Number of Items: {0}", foo);