Porting from php 5.6 to 8.2, how to express auto/init of multidimensional array? - php-8

This is now illegal if $a is not initialized at each level.
$a["a"]["b"]["c"] +=1;
Does exists a syntax to force php8.2 to behave like php5.6 and so auto-initialize the $a["a"]["b"]["c"] to value zero, if and only if not already defined to each level?

You can use the Null coalescing operator ?? (as of PHP 7.0), to use 0 is any element of the chain is not defined.
Example:
$a['a']['b']['c'] = ($a['a']['b']['c'] ?? 0) + 1;
echo json_encode($a), PHP_EOL; // a.b.c = 1
$a['a']['b']['c'] = ($a['a']['b']['c'] ?? 0) + 1;
echo json_encode($a), PHP_EOL; // a.b.c = 2
Output:
{"a":{"b":{"c":1}}}
{"a":{"b":{"c":2}}}
https://3v4l.org/sdX97

Related

swift: about ternary operator Question. Why my code is error code??? Please tell me why I'm wrong

swift: about ternary operator Question. Why my code is error code??? Please tell me why I'm wrong.
var arr = [0,1,2,3,4,5,6,7,8]
var result = 0;
for a in 0..<arr.count{
for b in 1..<arr.count - 1{
for c in 2..<arr.count - 2 {
arr[a] + arr[b] + arr[c] <= input[1] ? result = arr[a] + arr[b] +arr[c] : continue
}
}
}
[this is my error]
[1]: https://i.stack.imgur.com/UdiUB.png
In Swift, the ternary condition operator is an expression which takes the form
<condition> ? <expression if true> : <expression if false>
Expressions are part of larger statements, and the ternary specifically is one which evaluates to either the expression after the ?, or the one after the : depending on the truth of the condition.
continue, however, is not an expression but a statement on its own, which means that it cannot be on either side of the ternary.
Thinking about this another way: expressions evaluate to some value (e.g., can be put on the right-hand-side of an assignment, like x = <some expression>), while statements do not (e.g., it doesn't make sense to write x = continue).
You will need to express this in the form of a regular if-statement then:
if arr[a] + arr[b] + arr[c] <= input[1] {
result = arr[a] + arr[b] +arr[c]
} else {
continue
}
Note that the above code might be grammatically correct (in that it will compile), but it is unlikely to be what you mean: the loop will automatically continue at the end of execution even if arr[a] + arr[b] + arr[c] <= input[1] by default, which means that your result may get overwritten later in the loop. It seems likely that you mean something like
outer_loop: for a in 0 ..< arr.count {
for b in 1 ..< arr.count - 1 {
for c in 2 ..< arr.count - 2 {
if arr[a] + arr[b] + arr[c] <= input[1] {
result = arr[a] + arr[b] + arr[c]
// `break` would only exit the `c` loop, but with this label
// we can exit all loops at once.
break outer_loop
}
}
}
}

How to print ONCE if there are multiple correct answers? (MATLAB)

So I have arr = randi([0,20],20,1). I want to show: If there are numbers less than 5, fprintf('Yes\n') only once. Im using a for loop (for i = 1 : length(arr)) and indexing it.
As your description, maybe you need if statement within for loop like below
for i = 1:length(arr)
if arr(i) < 5
fprintf('Yes\n');
break
end
end
If you want to print Yes once, you can try
if any(arr < 5)
fprintf('Yes\n')
endif
If you don't want to use break, the code below might be an option
for i = 1:min(find(arr <5))
if (arr(i) < 5)
fprintf('Yes\n');
end
end
You can use a break statement upon finding the first value under 5 and printing the Yes statement.
Using a break Statement:
arr = randi([0,20],20,1);
for i = 1: length(arr)
if arr(i) < 5
fprintf("Yes\n");
break;
end
end
Extension:
By Using any() Function:
Alternatively, if you'd like to concise it down without the need for a for-loop the any() function can be used to determine if any values within the array meet a condition in this case arr < 5.
arr = randi([0,20],20,1);
if(any(arr < 5))
fprintf("Yes\n");
end
By Using a While Loop:
Check = 0;
arr = randi([0,20],20,1);
i = 1;
while (Check == 0 && i < length(arr))
if arr(i) < 5
fprintf("Yes\n");
Check = 1;
end
i = i + 1;
end

Mapping binary data in perl

I have the following predefined codes that represent an index in a binary bitmap:
0 = standard
1 = special
2 = regular
3 = late
4 = early
5 = on time
6 = generic
7 = rfu
An example value I would take as an input would be 213, which becomes 11010101 in binary. Index 0, 2, 4, 6, and 7 have their bit flipped indicating that this record is:
standard + regular + early + generic + rfu.
I am trying to figure out in perl how to take that binary data and build a string, like mentioned above with code + code + code, etc.
Any help would be greatly appreciated. Thanks.
Edit: My thoughts on how I might approach this are:
Convert decimal to binary
Find length of binary string
Using substr get the value (0 or 1) index by index
If index value = 1 then add relevant code to string
Is there a better way to go about this?
You can test bits on input from 0 to 7, and take only these that are set,
my $in = 213;
my #r = ("standard","special","regular","late","early","on time","generic","rfu");
print join " + ", #r[ grep { $in & (1 << $_) } 0 .. $#r ];
# or
# print join " + ", map { $in & (1<<$_) ? $r[$_] : () } 0 .. $#r;
output
standard + regular + early + generic + rfu

Illegal division by zero

#xyVal = (4,4,6,6,10,12,18,22,24,28,30);
#yVal = (176,178,180,184,192,202,210,218,224,232,238);
#xxVal = (9,9,9,9,9 ,11,13,15,17,19,19);
#xVal = (168,166,164,162,158,150,142,134,122,116,110);
for ($i = 0; $i < scalar(#xVal); $i++){
for ($i = 0; #xyVal[$i] < #xxVal[$i]; $i++){
#yNewVal = #yVal[$i-1] + (#yVal[$i] - #yVal[$i-1])*(#xxVal[$i] - #xyVal[$i-1])/(#xyVal[$i] - #xyVal[$i-1]);
}
}
print #yNewVal;
I understand why its giving me the error Illegal division by zero about line 9 (the #yNewVal = ...)
I want the array to have 0 in it if there is a division between zeros. What am I doing wrong? So, how can I avoid that my application crashes when there is a division by zero?
Your divisor on that line is #xyVal[$i] - #xyVal[$i-1], so any case where you have two identical adjacent values in #xyVAl (e.g. 4,4) will result in a 0, and thus a divide-by-zero error.
You could say:
#yNewVal = ($_ = #xyVal[$i] - #xyVal[$i-1]) == 0 ? 0 : #yVal[$i-1] + (#yVal[$i] - #yVal[$i-1])*(#xxVal[$i] - #xyVal[$i-1])/$_;
well if i understand you correctly:
if (#xyVal[$i] == #xyVal[$i-1])
#yNewVal = 0;
else
#yNewVal = #yVal[$i-1] + (#yVal[$i] - #yVal[$i-1])*(#xxVal[$i] - #xyVal[$i-1])/(#xyVal[$i] - #xyVal[$i-1]);
You can perform a try/catch using eval and conditional operators.
eval {
#yNewVal = #yVal[$i-1] + (#yVal[$i] - #yVal[$i-1])*(#xxVal[$i] - #xyVal[$i-1])/(#xyVal[$i] - #xyVal[$i-1]);
1;
} or do {
#yNewVal = (0);
};
print #yNewVal;
Though, your phrase is returning a scalar value and putting it into an array variable. So you may want to re-factor that.

Is it possible to write loop in Scala Console?

I try to test this in Scala Console (I mean console not script file):
while i < 10 {print(i) i += 1}
It doesn't work. I tried multiple lines it doesn't seem to either.
Am I obliged to use a script file just to test a simple loop ?
Yes it's possible. You have some syntax errors though:
var i = 0
while (i < 10) { println(i); i += 1 }
Or on multiple lines:
var i = 0
while (i < 10) {
println(i)
i += 1
}
As usual, there is more than one way to do this:
// join values beforehand and print the string in one go
println(0 to 9 mkString("\n"))
// using foreach
(0 to 9).foreach(println)
// using for
for(i <- 0 to 9) println(i)
scala> while i < 10 {print(i) i += 1}
<console>:1: error: '(' expected but identifier found.
while i < 10 {print(i) i += 1}
^
As indicated by the error message, a while must be followed by an "(", as the condition it tests for must be enclosed inside parenthesis. The same thing holds true for "if" and "for", by the way.
What you want is this:
var i = 0; while (i < 10) { print(i); i += 1 };
On the other hand, Scala encourages you to not use a mutable variable and while + condition
If you want to print the numbers from 0 to 9, use a sequence comprehension :
for (var <- range ) doSomethingWith (var)
In your case will be:
for (i <- 0 to 9) print (i)
(yes, the example looks pretty silly, but it helps to transition to a more "Scalaish" code)