tag:blogger.com,1999:blog-25760276130257284322024-03-13T13:32:10.193-07:00PHPFixingTedhttp://www.blogger.com/profile/17019839626603513962noreply@blogger.comBlogger21608125tag:blogger.com,1999:blog-2576027613025728432.post-24968728379495154652022-12-22T15:38:00.001-08:002022-12-22T15:38:09.077-08:00[FIXED] How can I solve the error -- error: invalid types ‘int[int]’ for array subscript?
<div id='dv1'></div>
<h1>Issue</h1>
<div id='dv2'></div>
<pre><code>#include <iostream>
#include <iomanip>
using namespace std;
int col=10;
int row=0;
void avg(int * ar,int row, int col)
{
float size= row * col;
int sum=0, ave;
for(int i=0; i<row; i++)
{
for(int j=0; j<col; j++){
sum+=ar[i][j];
cout<<sum;}
}
ave=sum/size;
cout<<sum<<endl;
cout<<ave;
}
int main()
{
int row, col;
cout<<"How many rows does the 2D array have: ";
cin>>row;
cout<<"How many columns does the 2D array have: ";
cin>>col;
int ar[row][col];
cout<<"Enter the 2D array elements below : \n";
for(int i=0; i<row; i++){
cout<<"For row "<<i + 1<<" : \n";
for(int j=0; j<col; j++)
cin>>ar[i][j];
}
cout<<"\n Array is: \n";
for(int i=0; i<row; i++)
{
for(int j=0; j<col; j++)
cout<<setw(6)<<ar[i][j];
cout<<endl;
}
cout<<"\nAverage of all the elements of the given D array is: \n";
avg((int*)ar,row,col);
return 0;
}
</code></pre>
<p>Hi there, I have written this code to calculate the average of the elements of an 2D array. I am getting error while trying to access the array elements of a 2D array at line 12 -13 ar[i][j]</p>
<p>The error says- error: invalid types ‘int[int]’ for array subscript</p>
<p><strong>How can I solve this error?</strong></p>
<p>PS: I want to give row( no. of rows in 2D array) and col(no. of columns in 2D array) in the function parameter to make this more dynamic.</p>
<div id='dv3'></div>
<br><div id='dv4'></div><h1>Solution</h1>
<div id='dv5'></div>
<p>Your function parameter <code>ar</code> is a <code>int*</code>. But when you wrote <code>sum+=ar[i][j]</code> you're subscripting it as if we had a 2D array. You can only subscript it for one dimension like <code>arr[i]</code>.</p>
<p><strong>Additionally</strong>, <code>row</code> and <code>col</code> are <strong>not</strong> constant expressions. And in Standard C++ the size of an array must be a compile time constant(constant expression). So,</p>
<pre><code>int ar[row][col]; //this statement is not standard c++
</code></pre>
<p>The above statement is <strong>not standard c++</strong>.</p>
<p>A better way(to avoid these problems) would be to use a 2D <code>std::vector</code> instead of a 2D array as shown below.</p>
<pre><code>#include <iostream>
#include <iomanip>
#include <vector>
//this function takes a 2D vector by reference and returns a double value
double avg(const std::vector<std::vector<int>> &arr)
{
int sum=0;
for(const std::vector<int> &tempRow: arr)
{
for(const int &tempCol: tempRow){
sum+=tempCol;
//std::cout<<sum;
}
}
return (static_cast<double>(sum)/(arr.at(0).size() * arr.size()));
}
int main()
{
int row, col;
std::cout<<"How many rows does the 2D array have: ";
std::cin>>row;
std::cout<<"How many columns does the 2D array have: ";
std::cin>>col;
//create a 2D vector instead of array
std::vector<std::vector<int>> ar(row, std::vector<int>(col));
std::cout<<"Enter the 2D array elements below : \n";
for(auto &tempRow: ar){
for(auto &tempCol: tempRow){
std::cin>>tempCol;
}
}
std::cout<<"\n Array is: \n";
for(auto &tempRow: ar)
{
for(auto &tempCol: tempRow)
std::cout<<std::setw(6)<<tempCol;
std::cout<<std::endl;
}
std::cout<<"\nAverage of all the elements of the given D array is: \n";
std::cout<<avg(ar);
return 0;
}
</code></pre>
<p>The output of the above program can be seen <a href="https://onlinegdb.com/t_ww9EvncB" rel="nofollow noreferrer">here</a>.</p>
<div id='dv6'></div>
<br>
<br>
<div id='dv7'></div>
Answered By - <a href="https://stackoverflow.com/questions/70750236/how-can-i-solve-the-error-error-invalid-types-intint-for-array-subscript/70750403#70750403">Jason Liam</a>
<div id='dv8'></div>
Answer Checked By - <a href="https://www.phpfixing.com/p/contact-us.html">Katrina (PHPFixing Volunteer)</a> <br>
<div id='dv9'></div>
Philhttp://www.blogger.com/profile/04542702300179882994noreply@blogger.com0tag:blogger.com,1999:blog-2576027613025728432.post-91919511307450442412022-12-22T14:26:00.001-08:002022-12-22T14:26:42.638-08:00[FIXED] How do I perform a GROUP BY on an aliased column in SQL Server?
<div id='dv1'></div>
<h1>Issue</h1>
<div id='dv2'></div>
<p>I'm trying to perform a <em>group by</em> action on an aliased column (example below) but can't determine the proper syntax.</p>
<pre><code>SELECT LastName + ', ' + FirstName AS 'FullName'
FROM customers
GROUP BY 'FullName'
</code></pre>
<p>What is the correct syntax?</p>
<p>Extending the question further (I had not expected the answers I had received) would the solution still apply for a CASEed aliased column?</p>
<pre><code>SELECT
CASE
WHEN LastName IS NULL THEN FirstName
WHEN LastName IS NOT NULL THEN LastName + ', ' + FirstName
END AS 'FullName'
FROM customers
GROUP BY
LastName, FirstName
</code></pre>
<p>And the answer is yes it does still apply.</p>
<div id='dv3'></div>
<br><div id='dv4'></div><h1>Solution</h1>
<div id='dv5'></div>
<p>You pass the expression you want to group by rather than the alias </p>
<pre><code>SELECT LastName + ', ' + FirstName AS 'FullName'
FROM customers
GROUP BY LastName + ', ' + FirstName
</code></pre>
<div id='dv6'></div>
<br>
<br>
<div id='dv7'></div>
Answered By - <a href="https://stackoverflow.com/questions/497241/how-do-i-perform-a-group-by-on-an-aliased-column-in-sql-server/497251#497251">cmsjr</a>
<div id='dv8'></div>
Answer Checked By - <a href="https://www.phpfixing.com/p/contact-us.html">David Goodson (PHPFixing Volunteer)</a> <br>
<div id='dv9'></div>
Philhttp://www.blogger.com/profile/04542702300179882994noreply@blogger.com0tag:blogger.com,1999:blog-2576027613025728432.post-12549198018501736892022-12-22T13:03:00.001-08:002022-12-22T13:03:16.784-08:00[FIXED] How to do complex conditionals in Bash (mix of 'and' &&, 'or' || ...)
<div id='dv1'></div>
<h1>Issue</h1>
<div id='dv2'></div>
<p>How do I accomplish something like the following in Bash?</p>
<pre><code>if ("$a" == "something" || ($n == 2 && "$b" == "something_else")); then
...
fi
</code></pre>
<div id='dv3'></div>
<br><div id='dv4'></div><h1>Solution</h1>
<div id='dv5'></div>
<p>You almost got it:</p>
<pre><code>if [[ "$a" == "something" || ($n == 2 && "$b" == "something_else") ]]; then
</code></pre>
<p>In fact, the parentheses can be left out because of operator precedence, so it might also be written as</p>
<pre><code>if [[ "$a" == "something" || $n == 2 && "$b" == "something_else" ]]; then
</code></pre>
<div id='dv6'></div>
<br>
<br>
<div id='dv7'></div>
Answered By - <a href="https://stackoverflow.com/questions/10288959/how-to-do-complex-conditionals-in-bash-mix-of-and-or/10289002#10289002">Niklas B.</a>
<div id='dv8'></div>
Answer Checked By - <a href="https://www.phpfixing.com/p/contact-us.html">Timothy Miller (PHPFixing Admin)</a> <br>
<div id='dv9'></div>
Philhttp://www.blogger.com/profile/04542702300179882994noreply@blogger.com0tag:blogger.com,1999:blog-2576027613025728432.post-46464960262327333242022-12-22T12:14:00.001-08:002022-12-22T12:14:20.096-08:00[FIXED] What difference between passing an instance and a brace-enclosed initializer list to a function?
<div id='dv1'></div>
<h1>Issue</h1>
<div id='dv2'></div>
<p>Say I have the following code.</p>
<pre><code>class A
{
public:
float x, y, z;
A(float x, float y, float z) : x(x), y(y), z(z) {}
};
float average(A a)
{
return (a.x + a.y + a.z) / 3;
}
</code></pre>
<p>Are there any practical differences between calling the function in these two ways?</p>
<pre><code>// a)
average(A(1, 2, 3))
// b)
average({1, 2, 3})
</code></pre>
<div id='dv3'></div>
<br><div id='dv4'></div><h1>Solution</h1>
<div id='dv5'></div>
<p>It depends on the C++ version you are using.</p>
<p>Copied (and sligthly modified) from <a href="https://en.cppreference.com/w/cpp/language/copy_elision" rel="nofollow noreferrer">copy elision @ cpprefrence</a> (so, not an exact quote):</p>
<blockquote>
<p>In C++17 core language specification of <code>prvalues</code> and temporaries is fundamentally different from that of the earlier C++ revisions: there is no longer a temporary to copy/move from.</p>
</blockquote>
<p>This means that the temporary <code>A</code> you visually show in <code>average(A(1, 2, 3))</code> is not allowed to be copied or moved since C++17. In fact, there is no temporary. Before C++17, most compilers utilized <em>copy (and move) elision</em> (that has been allowed earlier too, but it wasn't mandatory) to the same effect - but you could not portably rely on it.</p>
<p>In C++17, this form of <em>copy elision</em> was made mandatory and is since called <em>Return Value Optimization</em>, or <em>RVO</em> for short. You will also read about <em>Named</em> RVO (or <em>NRVO</em>), which is similar, but does not follow the same rules. It's a bit more complicated and outside the scope of this answer.</p>
<p>Before C++17, <code>average(A(1, 2, 3))</code> could actually mean that a temporary instance of <code>A</code> is created and that the <em>move</em> or <em>copy</em> constructor of the <code>A</code> in the function would have to move (or for a non-movable, copy) the resources from that temporary - which is usually cheap, but doesn't have to be - and it could come with side effects.</p>
<p>Some types are neither copyable nor movable. In such cases, the call would not be valid before C++17. In C++17, <code>delete</code>d <em>move</em> and <em>copy</em> constructors does not matter in this case since there is no instance to move or copy from - and the call <em>is</em> therefore undoubtedly valid.</p>
<p>When using the brace enclosed initializer list, as you do in this example, not even C++11 is allowed to create (and move) a temporary instance of <code>A</code>. The values are passed directly to the constructor in <code>A(float x, float y, float z)</code>.</p>
<p>Other that that, I can't spot a difference in your example.</p>
<div id='dv6'></div>
<br>
<br>
<div id='dv7'></div>
Answered By - <a href="https://stackoverflow.com/questions/70818401/what-difference-between-passing-an-instance-and-a-brace-enclosed-initializer-lis/70818547#70818547">Ted Lyngmo</a>
<div id='dv8'></div>
Answer Checked By - <a href="https://www.phpfixing.com/p/contact-us.html">David Goodson (PHPFixing Volunteer)</a> <br>
<div id='dv9'></div>
Philhttp://www.blogger.com/profile/04542702300179882994noreply@blogger.com0tag:blogger.com,1999:blog-2576027613025728432.post-23957662260390137302022-12-22T11:55:00.001-08:002022-12-22T11:55:20.223-08:00[FIXED] What difference between passing an instance and a brace-enclosed initializer list to a function?
<div id='dv1'></div>
<h1>Issue</h1>
<div id='dv2'></div>
<p>Say I have the following code.</p>
<pre><code>class A
{
public:
float x, y, z;
A(float x, float y, float z) : x(x), y(y), z(z) {}
};
float average(A a)
{
return (a.x + a.y + a.z) / 3;
}
</code></pre>
<p>Are there any practical differences between calling the function in these two ways?</p>
<pre><code>// a)
average(A(1, 2, 3))
// b)
average({1, 2, 3})
</code></pre>
<div id='dv3'></div>
<br><div id='dv4'></div><h1>Solution</h1>
<div id='dv5'></div>
<p>It depends on the C++ version you are using.</p>
<p>Copied (and sligthly modified) from <a href="https://en.cppreference.com/w/cpp/language/copy_elision" rel="nofollow noreferrer">copy elision @ cpprefrence</a> (so, not an exact quote):</p>
<blockquote>
<p>In C++17 core language specification of <code>prvalues</code> and temporaries is fundamentally different from that of the earlier C++ revisions: there is no longer a temporary to copy/move from.</p>
</blockquote>
<p>This means that the temporary <code>A</code> you visually show in <code>average(A(1, 2, 3))</code> is not allowed to be copied or moved since C++17. In fact, there is no temporary. Before C++17, most compilers utilized <em>copy (and move) elision</em> (that has been allowed earlier too, but it wasn't mandatory) to the same effect - but you could not portably rely on it.</p>
<p>In C++17, this form of <em>copy elision</em> was made mandatory and is since called <em>Return Value Optimization</em>, or <em>RVO</em> for short. You will also read about <em>Named</em> RVO (or <em>NRVO</em>), which is similar, but does not follow the same rules. It's a bit more complicated and outside the scope of this answer.</p>
<p>Before C++17, <code>average(A(1, 2, 3))</code> could actually mean that a temporary instance of <code>A</code> is created and that the <em>move</em> or <em>copy</em> constructor of the <code>A</code> in the function would have to move (or for a non-movable, copy) the resources from that temporary - which is usually cheap, but doesn't have to be - and it could come with side effects.</p>
<p>Some types are neither copyable nor movable. In such cases, the call would not be valid before C++17. In C++17, <code>delete</code>d <em>move</em> and <em>copy</em> constructors does not matter in this case since there is no instance to move or copy from - and the call <em>is</em> therefore undoubtedly valid.</p>
<p>When using the brace enclosed initializer list, as you do in this example, not even C++11 is allowed to create (and move) a temporary instance of <code>A</code>. The values are passed directly to the constructor in <code>A(float x, float y, float z)</code>.</p>
<p>Other that that, I can't spot a difference in your example.</p>
<div id='dv6'></div>
<br>
<br>
<div id='dv7'></div>
Answered By - <a href="https://stackoverflow.com/questions/70818401/what-difference-between-passing-an-instance-and-a-brace-enclosed-initializer-lis/70818547#70818547">Ted Lyngmo</a>
<div id='dv8'></div>
Answer Checked By - <a href="https://www.phpfixing.com/p/contact-us.html">Timothy Miller (PHPFixing Admin)</a> <br>
<div id='dv9'></div>
Philhttp://www.blogger.com/profile/04542702300179882994noreply@blogger.com0tag:blogger.com,1999:blog-2576027613025728432.post-71157368564047687392022-12-22T10:58:00.001-08:002022-12-22T10:58:43.049-08:00[FIXED] What does it mean to pass `_` (i.e., underscore) as the sole parameter to a Dart language function?
<div id='dv1'></div>
<h1>Issue</h1>
<div id='dv2'></div>
<p>I'm learning Dart and see the following idiom a lot:</p>
<p><code>someFuture.then((_) => someFunc());</code></p>
<p>I have also seen code like:</p>
<p><code>someOtherFuture.then(() => someOtherFunc());</code></p>
<p>Is there a functional difference between these two examples?
A.k.a., What does passing <code>_</code> as a parameter to a Dart function do?</p>
<p>This is particularly confusing given Dart's use of <code>_</code> as a prefix for declaring private functions.</p>
<div id='dv3'></div>
<br><div id='dv4'></div><h1>Solution</h1>
<div id='dv5'></div>
<p>It's a variable named <code>_</code> typically because you plan to not use it and throw it away. For example you can use the name <code>x</code> or <code>foo</code> instead.
The difference between <code>(_)</code> and <code>()</code> is simple in that one function takes an argument and the other doesn't.</p>
<blockquote>
<p><strong>DON’T use a leading underscore for identifiers that aren’t private.</strong></p>
<p><strong>Exception:</strong> An unused parameter can be named _, __, ___, etc. This
happens in things like callbacks where you are passed a value but you
don’t need to use it. Giving it a name that consists solely of
underscores is the idiomatic way to indicate the value isn’t used.</p>
</blockquote>
<p><a href="https://dart.dev/guides/language/effective-dart/style" rel="noreferrer">https://dart.dev/guides/language/effective-dart/style</a></p>
<div id='dv6'></div>
<br>
<br>
<div id='dv7'></div>
Answered By - <a href="https://stackoverflow.com/questions/25517016/what-does-it-mean-to-pass-i-e-underscore-as-the-sole-parameter-to-a-dart/25517120#25517120">Zectbumo</a>
<div id='dv8'></div>
Answer Checked By - <a href="https://www.phpfixing.com/p/contact-us.html">Robin (PHPFixing Admin)</a> <br>
<div id='dv9'></div>
Philhttp://www.blogger.com/profile/04542702300179882994noreply@blogger.com0tag:blogger.com,1999:blog-2576027613025728432.post-57546033551105747372022-12-22T10:17:00.001-08:002022-12-22T10:17:47.188-08:00[FIXED] What does %>% function mean in R?
<div id='dv1'></div>
<h1>Issue</h1>
<div id='dv2'></div>
<p>I have seen the use of <code>%>%</code> (percent greater than percent) function in some packages like <strong><a href="https://github.com/hadley/dplyr" rel="noreferrer">dplyr</a></strong> and <strong><a href="https://github.com/hadley/rvest" rel="noreferrer">rvest</a></strong>. What does it mean? Is it a way to write closure blocks in R?</p>
<div id='dv3'></div>
<br><div id='dv4'></div><h1>Solution</h1>
<div id='dv5'></div>
<h2>%...% operators</h2>
<p><code>%>%</code> has no builtin meaning but the user (or a package) is free to define operators of the form <code>%whatever%</code> in any way they like. For example, this function will return a string consisting of its left argument followed by a comma and space and then it's right argument.</p>
<pre><code>"%,%" <- function(x, y) paste0(x, ", ", y)
# test run
"Hello" %,% "World"
## [1] "Hello, World"
</code></pre>
<p>The base of R provides <code>%*%</code> (matrix mulitiplication), <code>%/%</code> (integer division), <code>%in%</code> (is lhs a component of the rhs?), <code>%o%</code> (outer product) and <code>%x%</code> (kronecker product). It is not clear whether <code>%%</code> falls in this category or not but it represents modulo.</p>
<p><strong>expm</strong> The R package, expm, defines a matrix power operator <code>%^%</code>. For an example see <a href="https://stackoverflow.com/questions/3274818/matrix-power-in-r">Matrix power in R</a> .</p>
<p><strong>operators</strong> The operators R package has defined a large number of such operators such as <code>%!in%</code> (for not <code>%in%</code>). See <a href="http://cran.r-project.org/web/packages/operators/operators.pdf" rel="noreferrer">http://cran.r-project.org/web/packages/operators/operators.pdf</a></p>
<p><strong>igraph</strong> This package defines %--% , %->% and %<-% to select edges.</p>
<p><strong>lubridate</strong> This package defines %m+% and %m-% to add and subtract months and %--% to define an interval. igraph also defines %--% .</p>
<h2>Pipes</h2>
<p><strong>magrittr</strong> In the case of <code>%>%</code> the magrittr R package has defined it as discussed in the magrittr vignette. See <a href="http://cran.r-project.org/web/packages/magrittr/vignettes/magrittr.html" rel="noreferrer">http://cran.r-project.org/web/packages/magrittr/vignettes/magrittr.html</a></p>
<p>magittr has also defined a number of other such operators too. See the Additional Pipe Operators section of the prior link which discusses <code>%T>%</code>, <code>%<>%</code> and <code>%$%</code> and <a href="http://cran.r-project.org/web/packages/magrittr/magrittr.pdf" rel="noreferrer">http://cran.r-project.org/web/packages/magrittr/magrittr.pdf</a> for even more details.</p>
<p><strong>dplyr</strong> The dplyr R package used to define a <code>%.%</code> operator which is similar; however, it has been deprecated and dplyr now recommends that users use <code>%>%</code> which dplyr imports from magrittr and makes available to the dplyr user. As David Arenburg has mentioned in the comments this SO question discusses the differences between it and magrittr's <code>%>%</code> : <a href="https://stackoverflow.com/questions/23621209/differences-between-dplyr-and-magrittr">Differences between %.% (dplyr) and %>% (magrittr)</a></p>
<p><strong>pipeR</strong> The R package, pipeR, defines a <code>%>>%</code> operator that is similar to magrittr's %>% and can be used as an alternative to it. See <a href="http://renkun.me/pipeR-tutorial/" rel="noreferrer">http://renkun.me/pipeR-tutorial/</a></p>
<p>The pipeR package also has defined a number of other such operators too. See: <a href="http://cran.r-project.org/web/packages/pipeR/pipeR.pdf" rel="noreferrer">http://cran.r-project.org/web/packages/pipeR/pipeR.pdf</a></p>
<p><strong>postlogic</strong> The postlogic package defined <code>%if%</code> and <code>%unless%</code> operators.</p>
<p><strong>wrapr</strong> The R package, wrapr, defines a dot pipe <code>%.>%</code> that is an explicit version of <code>%>%</code> in that it does not do implicit insertion of arguments but only substitutes explicit uses of dot on the right hand side. This can be considered as another alternative to <code>%>%</code>. See <a href="https://winvector.github.io/wrapr/articles/dot_pipe.html" rel="noreferrer">https://winvector.github.io/wrapr/articles/dot_pipe.html</a></p>
<p><strong>Bizarro pipe</strong>. This is not really a pipe but rather some clever base syntax to work in a way similar to pipes without actually using pipes. It is discussed in <a href="http://www.win-vector.com/blog/2017/01/using-the-bizarro-pipe-to-debug-magrittr-pipelines-in-r/" rel="noreferrer">http://www.win-vector.com/blog/2017/01/using-the-bizarro-pipe-to-debug-magrittr-pipelines-in-r/</a> The idea is that instead of writing:</p>
<pre><code>1:8 %>% sum %>% sqrt
## [1] 6
</code></pre>
<p>one writes the following. In this case we explicitly use dot rather than eliding the dot argument and end each component of the pipeline with an assignment to the variable whose name is dot (<code>.</code>) . We follow that with a semicolon.</p>
<pre><code>1:8 ->.; sum(.) ->.; sqrt(.)
## [1] 6
</code></pre>
<p><strong>Update</strong> Added info on expm package and simplified example at top. Added postlogic package.</p>
<p><strong>Update 2</strong> The development version of R has defined a <code>|></code> pipe. Unlike magrittr's <code>%>%</code> it can only substitute into the first argument of the right hand side. Although limited, it works via syntax transformation so it has no performance impact.</p>
<div id='dv6'></div>
<br>
<br>
<div id='dv7'></div>
Answered By - <a href="https://stackoverflow.com/questions/27125672/what-does-function-mean-in-r/27129032#27129032">G. Grothendieck</a>
<div id='dv8'></div>
Answer Checked By - <a href="https://www.phpfixing.com/p/contact-us.html">Terry (PHPFixing Volunteer)</a> <br>
<div id='dv9'></div>
Philhttp://www.blogger.com/profile/04542702300179882994noreply@blogger.com0tag:blogger.com,1999:blog-2576027613025728432.post-85599894564777560962022-12-22T09:41:00.001-08:002022-12-22T09:41:27.162-08:00[FIXED] What does ${ } mean in PHP syntax?
<div id='dv1'></div>
<h1>Issue</h1>
<div id='dv2'></div>
<p>I have used PHP for a long time, but I just saw something like,</p>
<pre><code>${ }
</code></pre>
<p>To be precise, I saw this in a PHP Mongo page:</p>
<pre><code>$m = new Mongo("mongodb://${username}:${password}@host");
</code></pre>
<p>So, what does <code>${ }</code> do? It is quite hard to search with Google or in the PHP documentation for characters like <code>$</code>, <code>{</code> and <code>}</code>.</p>
<div id='dv3'></div>
<br><div id='dv4'></div><h1>Solution</h1>
<div id='dv5'></div>
<p><code>${ }</code> (dollar sign curly bracket) is known as <a href="http://www.php.net/manual/en/language.types.string.php#language.types.string.parsing.simple" rel="noreferrer"><strong>Simple syntax</strong></a>.</p>
<blockquote>
<p>It provides a way to embed a variable, an array value, or an object
property in a string with a minimum of effort.</p>
<p>If a dollar sign ($) is encountered, the parser will greedily take as
many tokens as possible to form a valid variable name. Enclose the
variable name in curly braces to explicitly specify the end of the
name.</p>
<pre><code><?php
$juice = "apple";
echo "He drank some $juice juice.".PHP_EOL;
// Invalid. "s" is a valid character for a variable name, but the variable is $juice.
echo "He drank some juice made of $juices.";
// Valid. Explicitly specify the end of the variable name by enclosing it in braces:
echo "He drank some juice made of ${juice}s.";
?>
</code></pre>
<p>The above example will output:</p>
<pre><code>He drank some apple juice.
He drank some juice made of .
He drank some juice made of apples.
</code></pre>
</blockquote>
<div id='dv6'></div>
<br>
<br>
<div id='dv7'></div>
Answered By - <a href="https://stackoverflow.com/questions/5571624/what-does-mean-in-php-syntax/5571728#5571728">celiker</a>
<div id='dv8'></div>
Answer Checked By - <a href="https://www.phpfixing.com/p/contact-us.html">Katrina (PHPFixing Volunteer)</a> <br>
<div id='dv9'></div>
Philhttp://www.blogger.com/profile/04542702300179882994noreply@blogger.com0tag:blogger.com,1999:blog-2576027613025728432.post-36830941522344269922022-12-22T08:14:00.001-08:002022-12-22T08:14:17.079-08:00[FIXED] How to convert this function from javascript to python?
<div id='dv1'></div>
<h1>Issue</h1>
<div id='dv2'></div>
<p><strong>Does anybody know how to convert this javascript function to python ?</strong></p>
<p>javascript:</p>
<pre><code>function ding(t, a, e, n) {
return t > a && t <= e && (t += n % (e - a)) > e && (t = t - e + a), t
}
</code></pre>
<p>This is my try on doing so:</p>
<pre><code>def ding(t, a, e, n):
return t > a and t <= e and (t + n % (e - a)) > e and (t = (t - e + a)), t
</code></pre>
<p>It returns a <strong>syntax error</strong> at the "=" in <code>(t = (t - e + a)) </code> and idk how to solve this right.</p>
<p>When giving it these values: <code>ding(53, 47, 57, 97) </code> it should return 50 in the original javascript function.</p>
<div id='dv3'></div>
<br><div id='dv4'></div><h1>Solution</h1>
<div id='dv5'></div>
<p>Does it have to be a one-liner? Why not just split it into a few lines:</p>
<pre class="lang-py prettyprint-override"><code>def ding(t, a, e, n):
if t > a and t <= e:
t += n % (e - a)
if t > e:
t -= e - a
return t
print(ding(53, 47, 57, 97)) # 50
</code></pre>
<div id='dv6'></div>
<br>
<br>
<div id='dv7'></div>
Answered By - <a href="https://stackoverflow.com/questions/70839991/how-to-convert-this-function-from-javascript-to-python/70840057#70840057">lusc</a>
<div id='dv8'></div>
Answer Checked By - <a href="https://www.phpfixing.com/p/contact-us.html">Candace Johnson (PHPFixing Volunteer)</a> <br>
<div id='dv9'></div>
Philhttp://www.blogger.com/profile/04542702300179882994noreply@blogger.com0tag:blogger.com,1999:blog-2576027613025728432.post-70132759171757866332022-12-22T07:46:00.001-08:002022-12-22T07:46:56.292-08:00[FIXED] What is "0is" notation in Rust?
<div id='dv1'></div>
<h1>Issue</h1>
<div id='dv2'></div>
<p>As seen in this repository:</p>
<p><a href="https://github.com/ReactiveX/RxRust/blob/master/src/lib.rs#L110" rel="nofollow noreferrer">https://github.com/ReactiveX/RxRust/blob/master/src/lib.rs#L110</a></p>
<pre><code>let gen = move |:| {
let it = range(0is, 20is);
// ~~~ ~~~~
let q = Box::new(Decoupler::new(dtx.clone()));
let mut map1 = Box::new(Map::new(|i : isize| {i * 10}));
let mut map2 = Box::new(Map::new(|i : isize| {i + 2}));
let mut iter = Box::new(IterPublisher::new(it));
map2.subscribe(q);
map1.subscribe(map2);
iter.subscribe(map1);
};
</code></pre>
<p>(squiggly emphasis mine)</p>
<p>I'm trying to figure out what <code>is</code> after a numeral is. The Book says about literal suffixes only briefly:</p>
<blockquote>
<p>Note that all number literals except the byte literal allow a type suffix, such as 57u8, and _ as a visual separator, such as 1_000.</p>
<p>— <a href="https://doc.rust-lang.org/book/ch03-02-data-types.html#integer-types" rel="nofollow noreferrer">https://doc.rust-lang.org/book/ch03-02-data-types.html#integer-types</a></p>
</blockquote>
<p>And the compiler (1.53) only understands a set of specific suffixes, so I could not even get the original crate built on my machine:</p>
<pre class="lang-none prettyprint-override"><code>invalid suffix `is`
help: the suffix must be one of the numeric types (`u32`, `isize`, `f32`, etc.)
</code></pre>
<p>Is it some sort of archaic syntax, or is it me missing something?</p>
<div id='dv3'></div>
<br><div id='dv4'></div><h1>Solution</h1>
<div id='dv5'></div>
<p>In the old, pre 1.0, times, integer suffixes were a little different.</p>
<p>Thanks to the <a href="https://web.archive.org/web/20150127084620/http://doc.rust-lang.org/reference.html#integer-literals" rel="noreferrer">Wayback Machine</a>, we can take a peek to the past:</p>
<blockquote>
<p>There are 10 valid values for an integer suffix: \</p>
<ul>
<li>The <strong><code>is</code></strong> and <strong><code>us</code></strong> suffixes give the literal type <code>isize</code> or <code>usize</code>, respectively.</li>
<li>Each of the signed and unsigned machine types <code>u8</code>, <code>i8</code>, <code>u16</code>, <code>i16</code>, <code>u32</code>, <code>i32</code>, <code>u64</code> and <code>i64</code> give the literal the corresponding machine type.</li>
</ul>
</blockquote>
<p>But in Rust 1.0 the first bullet went away and now you write <code>20isize</code> instead of <code>20is</code>.</p>
<div id='dv6'></div>
<br>
<br>
<div id='dv7'></div>
Answered By - <a href="https://stackoverflow.com/questions/68767869/what-is-0is-notation-in-rust/68768349#68768349">rodrigo</a>
<div id='dv8'></div>
Answer Checked By - <a href="https://www.phpfixing.com/p/contact-us.html">David Goodson (PHPFixing Volunteer)</a> <br>
<div id='dv9'></div>
Philhttp://www.blogger.com/profile/04542702300179882994noreply@blogger.com0tag:blogger.com,1999:blog-2576027613025728432.post-90526409878720741082022-12-22T07:09:00.001-08:002022-12-22T07:09:31.575-08:00[FIXED] What does 'wb' mean in this code, using Python?
<div id='dv1'></div>
<h1>Issue</h1>
<div id='dv2'></div>
<p>Code:</p>
<pre><code>file('pinax/media/a.jpg', 'wb')
</code></pre>
<div id='dv3'></div>
<br><div id='dv4'></div><h1>Solution</h1>
<div id='dv5'></div>
<p><a href="http://docs.python.org/tutorial/inputoutput.html#reading-and-writing-files" rel="noreferrer">File mode</a>, write and binary. Since you are writing a .jpg file, it looks fine. </p>
<p>But if you supposed to read that jpg file you need to use <code>'rb'</code></p>
<p>More info</p>
<blockquote>
<p>On Windows, 'b' appended to the mode
opens the file in binary mode, so
there are also modes like 'rb', 'wb',
and 'r+b'. Python on Windows makes a
distinction between text and binary
files; the end-of-line characters in
text files are automatically altered
slightly when data is read or written.
This behind-the-scenes modification to
file data is fine for ASCII text
files, but it’ll corrupt binary data
like that in JPEG or EXE files.</p>
</blockquote>
<div id='dv6'></div>
<br>
<br>
<div id='dv7'></div>
Answered By - <a href="https://stackoverflow.com/questions/2665866/what-does-wb-mean-in-this-code-using-python/2665873#2665873">YOU</a>
<div id='dv8'></div>
Answer Checked By - <a href="https://www.phpfixing.com/p/contact-us.html">Mary Flores (PHPFixing Volunteer)</a> <br>
<div id='dv9'></div>
Philhttp://www.blogger.com/profile/04542702300179882994noreply@blogger.com0tag:blogger.com,1999:blog-2576027613025728432.post-64839112686537751312022-12-22T06:13:00.001-08:002022-12-22T06:13:39.171-08:00[FIXED] How to check TypeScript code for syntax errors from a command line?
<div id='dv1'></div>
<h1>Issue</h1>
<div id='dv2'></div>
<p>I have a code that generates TypeScript classes, and as a build/test step, I would like to check the generated files for syntax correctness.</p>
<p>I have looked at <a href="https://www.typescriptlang.org/docs/handbook/compiler-options.html" rel="noreferrer">TypeScript compiler options</a> but see no such option.</p>
<ul>
<li>How can I check the syntax?</li>
</ul>
<p>I don't want a full compilation because the referred types are not reachable at that build step (they are in a different module to which the generated sources are added later).</p>
<div id='dv3'></div>
<br><div id='dv4'></div><h1>Solution</h1>
<div id='dv5'></div>
<p>If its just syntax checking you are after then you can use a linter like <a href="https://palantir.github.io/tslint/" rel="nofollow noreferrer">tslint</a> which can be run from the command line or via many build tools</p>
<div id='dv6'></div>
<br>
<br>
<div id='dv7'></div>
Answered By - <a href="https://stackoverflow.com/questions/41542907/how-to-check-typescript-code-for-syntax-errors-from-a-command-line/41547358#41547358">alechill</a>
<div id='dv8'></div>
Answer Checked By - <a href="https://www.phpfixing.com/p/contact-us.html">Pedro (PHPFixing Volunteer)</a> <br>
<div id='dv9'></div>
Philhttp://www.blogger.com/profile/04542702300179882994noreply@blogger.com0tag:blogger.com,1999:blog-2576027613025728432.post-14384037682363649612022-12-22T05:39:00.001-08:002022-12-22T05:39:34.006-08:00[FIXED] When to use : and = in javascript?
<div id='dv1'></div>
<h1>Issue</h1>
<div id='dv2'></div>
<p>I have these code snippet.</p>
<pre><code> const initialAuthState = {
isAuthenticated:false
};
</code></pre>
<p>and</p>
<pre><code>function login(state){
state.isAuthenticated=true;
}
</code></pre>
<p>How do I know when to use '=' and when to use ':' in javascript?</p>
<div id='dv3'></div>
<br><div id='dv4'></div><h1>Solution</h1>
<div id='dv5'></div>
<p>= inside a function or code block (like inside an <code>if</code>/<code>else</code>/<code>for</code>/<code>while</code> etc.), <code>:</code> inside of objects.</p>
<div id='dv6'></div>
<br>
<br>
<div id='dv7'></div>
Answered By - <a href="https://stackoverflow.com/questions/70874610/when-to-use-and-in-javascript/70874628#70874628">Samathingamajig</a>
<div id='dv8'></div>
Answer Checked By - <a href="https://www.phpfixing.com/p/contact-us.html">Gilberto Lyons (PHPFixing Admin)</a> <br>
<div id='dv9'></div>
Philhttp://www.blogger.com/profile/04542702300179882994noreply@blogger.com0tag:blogger.com,1999:blog-2576027613025728432.post-36698729609442765042022-12-22T02:00:00.001-08:002022-12-22T02:00:01.216-08:00[FIXED] How to select and view only a specific cell?
<div id='dv1'></div>
<h1>Issue</h1>
<div id='dv2'></div>
<p>I want to extract from the next code, only the first value. Unfortunately, I was unable and I didn't found similar answer for my question.
So, here is the example of script:</p>
**$1.61**
<p>With bold I mark the value which I am interested to be visible. Only this value in one cell. Unfortunately I didn't have need the rest of values from table.</p>
<p>Here I will add my function (function will be use ):</p>
<p>=importxml("https://www.coingecko.com/en/coins/polygon","//td/span")</p>
<p>Please let me know how how to select to view only 1st line/value.</p>
<p>Thanks to all for support.</p>
<div id='dv3'></div>
<br><div id='dv4'></div><h1>Solution</h1>
<div id='dv5'></div>
<p>This should select 1st <code>//td/span</code></p>
<pre><code>=importxml("https://www.coingecko.com/en/coins/polygon","(//td/span)[1]")
</code></pre>
<hr />
<p><strong>Reference</strong></p>
<p><a href="https://librarycarpentry.org/lc-webscraping/02-xpath/index.html" rel="nofollow noreferrer">https://librarycarpentry.org/lc-webscraping/02-xpath/index.html</a></p>
<div id='dv6'></div>
<br>
<br>
<div id='dv7'></div>
Answered By - <a href="https://stackoverflow.com/questions/70893043/how-to-select-and-view-only-a-specific-cell/70893374#70893374">Max Daroshchanka</a>
<div id='dv8'></div>
Answer Checked By - <a href="https://www.phpfixing.com/p/contact-us.html">Marilyn (PHPFixing Volunteer)</a> <br>
<div id='dv9'></div>
Philhttp://www.blogger.com/profile/04542702300179882994noreply@blogger.com0tag:blogger.com,1999:blog-2576027613025728432.post-59757163743957638622022-12-22T00:58:00.001-08:002022-12-22T00:58:00.117-08:00[FIXED] what is the right way to check that an object is not null in react before assigning one of its properties to a variable
<div id='dv1'></div>
<h1>Issue</h1>
<div id='dv2'></div>
<p>In React it is very commong that as components get updated objects go from null to their intended content. This is a dynamic process that can create errors everytime you assing a property from those objects to another variable.</p>
<p>Lets say that user is the object that will go from null to the actual user object and that I want to extract the property uid from user:</p>
<pre><code>const uid = user.uid
</code></pre>
<p>This can trigger a fatal error in react if user is null when I try to execute this operation.</p>
<p>I have tried to use:</p>
<p><code>user && const uid = user.uid</code>, but it doesn't work at all.</p>
<p>Would the right way to do this be:</p>
<pre><code>const uid = user && user.uid
</code></pre>
<p>any comments/ sugestions?</p>
<div id='dv3'></div>
<br><div id='dv4'></div><h1>Solution</h1>
<div id='dv5'></div>
<p><strong>The ?. operator is like the . chaining operator, except that instead of causing an error if a reference is nullish (null or undefined), the expression short-circuits with a return value of undefined</strong></p>
<p>So do this</p>
<pre class="lang-js prettyprint-override"><code>const uid = user?.uid // will be undefined if not there else the actually value
</code></pre>
<div id='dv6'></div>
<br>
<br>
<div id='dv7'></div>
Answered By - <a href="https://stackoverflow.com/questions/70899051/what-is-the-right-way-to-check-that-an-object-is-not-null-in-react-before-assign/70899142#70899142">carlosdafield</a>
<div id='dv8'></div>
Answer Checked By - <a href="https://www.phpfixing.com/p/contact-us.html"> Willingham (PHPFixing Volunteer)</a> <br>
<div id='dv9'></div>
Philhttp://www.blogger.com/profile/04542702300179882994noreply@blogger.com0tag:blogger.com,1999:blog-2576027613025728432.post-62944373288080832292022-12-22T00:15:00.001-08:002022-12-22T00:15:42.582-08:00[FIXED] How to swap text based on patterns at once with sed?
<div id='dv1'></div>
<h1>Issue</h1>
<div id='dv2'></div>
<p>Suppose I have 'abbc' string and I want to replace:</p>
<ul>
<li>ab -> bc</li>
<li>bc -> ab</li>
</ul>
<p>If I try two replaces the result is not what I want:</p>
<pre><code>echo 'abbc' | sed 's/ab/bc/g;s/bc/ab/g'
abab
</code></pre>
<p>So what sed command can I use to replace like below?</p>
<pre><code>echo abbc | sed SED_COMMAND
bcab
</code></pre>
<p><strong>EDIT</strong>:
Actually the text could have more than 2 patterns and I don't know how many replaces I will need. Since there was a answer saying that <code>sed</code> is a stream editor and its replaces are greedily I think that I will need to use some script language for that.</p>
<div id='dv3'></div>
<br><div id='dv4'></div><h1>Solution</h1>
<div id='dv5'></div>
<p>Maybe something like this:</p>
<pre><code>sed 's/ab/~~/g; s/bc/ab/g; s/~~/bc/g'
</code></pre>
<p>Replace <code>~</code> with a character that you know won't be in the string.</p>
<div id='dv6'></div>
<br>
<br>
<div id='dv7'></div>
Answered By - <a href="https://stackoverflow.com/questions/26568952/how-to-swap-text-based-on-patterns-at-once-with-sed/26568996#26568996">ooga</a>
<div id='dv8'></div>
Answer Checked By - <a href="https://www.phpfixing.com/p/contact-us.html">Senaida (PHPFixing Volunteer)</a> <br>
<div id='dv9'></div>
Philhttp://www.blogger.com/profile/04542702300179882994noreply@blogger.com0tag:blogger.com,1999:blog-2576027613025728432.post-27286651473856934682022-12-21T23:36:00.001-08:002022-12-21T23:36:06.361-08:00[FIXED] How to copy a map in golang?
<div id='dv1'></div>
<h1>Issue</h1>
<div id='dv2'></div>
<p>I can offer decomposition of map to 2 slices example:</p>
<pre><code>func decomposeMap(m map[string]int) ([]string, []int) {
var i uint
l := len(m)
keys, values := make([]string, l), make([]int, l)
for keys[i], values[i] = range m {
i++
}
return keys, values
}
</code></pre>
<p>but I am failing to write map copying:</p>
<pre><code>func copyMap(m map[string]int) map[string]int {
m2 := make(map[string]int, len(m))
for id, m2[id] = range m {} // error - id is not declared
for id, m2[id] := range m {} // error with m2[id] already declared
// id should not be accessible here, it should exist only inside loop
return m2
}
</code></pre>
<p>I can declare id as a var, but I dont want it to be available outside for loop. How can i mix assigment and declaration, eg: <code>for id:=, m[id]= range m {}</code> ?
So it will declare index just inside for loop, and will be not accessible outside?</p>
<div id='dv3'></div>
<br><div id='dv4'></div><h1>Solution</h1>
<div id='dv5'></div>
<p>The <code>id</code> variable must be declared before <code>for</code>, because you can't use short variable declaration with <code>m2[id]</code>.</p>
<pre><code>func copyMap(m map[string]int) map[string]int {
m2 := make(map[string]int, len(m))
var id string
for id, m2[id] = range m {
}
return m2
}
</code></pre>
<p><strong>But! This won't duplicate the map!</strong> The key is only assigned to <code>id</code> after <code>m2[id]</code> is already evaluated, so this loop will assign values to keys of the previous iteration, this is not duplicating, this is "shuffling"!</p>
<p>This is basically a tuple assignment (key and value are assigned to <code>id, m2[id]</code>). <a href="https://go.dev/ref/spec#Assignments" rel="nofollow noreferrer">Spec: Assignments:</a></p>
<blockquote>
<p>The assignment proceeds in two phases. First, the operands of <a href="https://go.dev/ref/spec#Index_expressions" rel="nofollow noreferrer">index expressions</a> and <a href="https://go.dev/ref/spec#Address_operators" rel="nofollow noreferrer">pointer indirections</a> (including implicit pointer indirections in <a href="https://go.dev/ref/spec#Selectors" rel="nofollow noreferrer">selectors</a>) on the left and the expressions on the right are all <a href="https://go.dev/ref/spec#Order_of_evaluation" rel="nofollow noreferrer">evaluated in the usual order</a>. Second, the assignments are carried out in left-to-right order.</p>
</blockquote>
<p>So first <code>id</code> and <code>m2[id]</code> are evaluated (including the index expression), so <code>id</code> is not "yet" changed, so the value from the previous iteration is used, and only after this are the new key and values assigned.</p>
<p>To demonstrate, see:</p>
<pre><code>m := map[string]int{
"one": 1,
"two": 2,
"three": 3,
}
m2 := copyMap(m)
fmt.Println(m2)
</code></pre>
<p>Output (try it on the <a href="https://go.dev/play/p/kgkfunoTf3_6" rel="nofollow noreferrer">Go Playground</a>):</p>
<pre><code>map[:1 one:2 two:3]
</code></pre>
<p>The values are assigned to different keys (different than in the source map), and one value is assigned to the empty string key in the first iteration (the default, zero value of <code>id</code>).</p>
<p>To duplicate the map, simply use:</p>
<pre><code>for id, v := range m {
m2[id] = v
}
</code></pre>
<p>Or if you want to avoid the temporary assignment:</p>
<pre><code>for id := range m {
m2[id] = m[id]
}
</code></pre>
<div id='dv6'></div>
<br>
<br>
<div id='dv7'></div>
Answered By - <a href="https://stackoverflow.com/questions/70925245/how-to-copy-a-map-in-golang/70925417#70925417">icza</a>
<div id='dv8'></div>
Answer Checked By - <a href="https://www.phpfixing.com/p/contact-us.html">Candace Johnson (PHPFixing Volunteer)</a> <br>
<div id='dv9'></div>
Philhttp://www.blogger.com/profile/04542702300179882994noreply@blogger.com0tag:blogger.com,1999:blog-2576027613025728432.post-90513752523088774312022-12-21T21:32:00.001-08:002022-12-21T21:32:19.369-08:00[FIXED] How to escape single quotes within single quoted strings
<div id='dv1'></div>
<h1>Issue</h1>
<div id='dv2'></div>
<p>Let's say, you have a Bash <code>alias</code> like:</p>
<pre><code>alias rxvt='urxvt'
</code></pre>
<p>which works fine.</p>
<p>However:</p>
<pre class="lang-none prettyprint-override"><code>alias rxvt='urxvt -fg '#111111' -bg '#111111''
</code></pre>
<p>won't work, and neither will:</p>
<pre><code>alias rxvt='urxvt -fg \'#111111\' -bg \'#111111\''
</code></pre>
<p>So how do you end up matching up opening and closing quotes inside a string once you have escaped quotes?</p>
<pre><code>alias rxvt='urxvt -fg'\''#111111'\'' -bg '\''#111111'\''
</code></pre>
<p>seems ungainly although it would represent the same string if you're allowed to concatenate them like that.</p>
<div id='dv3'></div>
<br><div id='dv4'></div><h1>Solution</h1>
<div id='dv5'></div>
<p>If you really want to use single quotes in the outermost layer, remember that you can glue both kinds of quotation. Example:</p>
<pre><code> alias rxvt='urxvt -fg '"'"'#111111'"'"' -bg '"'"'#111111'"'"
# ^^^^^ ^^^^^ ^^^^^ ^^^^
# 12345 12345 12345 1234
</code></pre>
<p>Explanation of how <code>'"'"'</code> is interpreted as just <code>'</code>:</p>
<ol>
<li><code>'</code> End first quotation which uses single quotes.</li>
<li><code>"</code> Start second quotation, using double-quotes.</li>
<li><code>'</code> Quoted character.</li>
<li><code>"</code> End second quotation, using double-quotes.</li>
<li><code>'</code> Start third quotation, using single quotes.</li>
</ol>
<p>If you do not place any whitespaces between (1) and (2), or between (4) and (5), the shell will interpret that string as a one long word.</p>
<div id='dv6'></div>
<br>
<br>
<div id='dv7'></div>
Answered By - <a href="https://stackoverflow.com/questions/1250079/how-to-escape-single-quotes-within-single-quoted-strings/1250279#1250279">liori</a>
<div id='dv8'></div>
Answer Checked By - <a href="https://www.phpfixing.com/p/contact-us.html">Katrina (PHPFixing Volunteer)</a> <br>
<div id='dv9'></div>
Philhttp://www.blogger.com/profile/04542702300179882994noreply@blogger.com0tag:blogger.com,1999:blog-2576027613025728432.post-78019567125682763782022-12-21T20:53:00.001-08:002022-12-21T20:53:11.973-08:00[FIXED] Why should I use a semicolon after every function in javascript?
<div id='dv1'></div>
<h1>Issue</h1>
<div id='dv2'></div>
<p>I've seen different developers include semicolons after functions in javascript and some haven't. Which is best practice?</p>
<pre><code>function weLikeSemiColons(arg) {
// bunch of code
};
</code></pre>
<p>or</p>
<pre><code>function unnecessary(arg) {
// bunch of code
}
</code></pre>
<div id='dv3'></div>
<br><div id='dv4'></div><h1>Solution</h1>
<div id='dv5'></div>
<p>Semicolons after <em>function declarations</em> are <strong>not necessary</strong>.</p>
<p>The grammar of a <code>FunctionDeclaration</code> is described in <a href="https://www.ecma-international.org/ecma-262/10.0/index.html#sec-ecmascript-language-functions-and-classes" rel="noreferrer">the specification</a> as this:</p>
<pre><code>function Identifier ( FormalParameterListopt ) { FunctionBody }
</code></pre>
<p>There's no semicolon grammatically required, but might wonder why?</p>
<p>Semicolons serve to separate <em>statements</em> from each other, and a <code>FunctionDeclaration</code> is not a <em>statement</em>.</p>
<p><code>FunctionDeclarations</code> are evaluated <em>before</em> the code enters into execution, <a href="https://stackoverflow.com/questions/1710424/referencing-a-javascript-value-before-it-is-declared-can-someone-explain-this/1710509#1710509">hoisting</a> is a <em>common word</em> used to explain this behaviour.</p>
<p>The terms "function declaration" and "function statement" are often wrongly used interchangeably, because there is no function statement described in the ECMAScript Specification, however there are some implementations that include a function statement in their grammar, -notably Mozilla- but again this is non-standard.</p>
<p>However, semicolons are always recommended where you use <code>FunctionExpressions</code>. For example:</p>
<pre><code>var myFn = function () {
//...
};
(function () {
//...
})();
</code></pre>
<p>If you omit the semicolon after the first function in the above example, you will get completely undesired results:</p>
<pre><code>var myFn = function () {
alert("Surprise!");
} // <-- No semicolon!
(function () {
//...
})();
</code></pre>
<p>The first function will be executed immediately, because the parentheses surrounding the second one will be interpreted as the <code>Arguments</code> of a function call.</p>
<p>Recommended lectures:</p>
<ul>
<li><a href="https://kangax.github.io/nfe/" rel="noreferrer">Named function expressions demystified</a> (great article)</li>
<li><a href="https://stackoverflow.com/questions/1634268/explain-javascripts-encapsulated-anonymous-function-syntax">Explain JavaScript’s encapsulated anonymous function syntax</a> (more on <code>FunctionDeclaration</code> vs <code>FunctionExpression</code>)</li>
</ul>
<div id='dv6'></div>
<br>
<br>
<div id='dv7'></div>
Answered By - <a href="https://stackoverflow.com/questions/1834642/why-should-i-use-a-semicolon-after-every-function-in-javascript/1834674#1834674">Christian C. Salvadó</a>
<div id='dv8'></div>
Answer Checked By - <a href="https://www.phpfixing.com/p/contact-us.html">Dawn Plyler (PHPFixing Volunteer)</a> <br>
<div id='dv9'></div>
Philhttp://www.blogger.com/profile/04542702300179882994noreply@blogger.com0tag:blogger.com,1999:blog-2576027613025728432.post-90563470692961205742022-12-21T19:57:00.001-08:002022-12-21T19:57:54.542-08:00[FIXED] How to concatenate strings in twig
<div id='dv1'></div>
<h1>Issue</h1>
<div id='dv2'></div>
<p>Anyone knows how to concatenate strings in twig? I want to do something like:</p>
<pre><code>{{ concat('http://', app.request.host) }}
</code></pre>
<div id='dv3'></div>
<br><div id='dv4'></div><h1>Solution</h1>
<div id='dv5'></div>
<p>This should work fine:</p>
<pre><code>{{ 'http://' ~ app.request.host }}
</code></pre>
<p>To add a filter - like 'trans' - in the same tag use</p>
<pre><code>{{ ('http://' ~ app.request.host) | trans }}
</code></pre>
<p>As <a href="https://stackoverflow.com/a/24693583/133717">Adam Elsodaney points out</a>, you can also use <a href="https://twig.symfony.com/doc/1.x/templates.html#string-interpolation" rel="noreferrer">string interpolation</a>, this does require double quoted strings:</p>
<pre><code>{{ "http://#{app.request.host}" }}
</code></pre>
<div id='dv6'></div>
<br>
<br>
<div id='dv7'></div>
Answered By - <a href="https://stackoverflow.com/questions/7704253/how-to-concatenate-strings-in-twig/7704889#7704889">Alessandro Desantis</a>
<div id='dv8'></div>
Answer Checked By - <a href="https://www.phpfixing.com/p/contact-us.html">Marie Seifert (PHPFixing Admin)</a> <br>
<div id='dv9'></div>
Philhttp://www.blogger.com/profile/04542702300179882994noreply@blogger.com0tag:blogger.com,1999:blog-2576027613025728432.post-40022413497366776292022-12-21T19:22:00.001-08:002022-12-21T19:22:33.484-08:00[FIXED] Why don't associated types for protocols use generic type syntax in Swift?
<div id='dv1'></div>
<h1>Issue</h1>
<div id='dv2'></div>
<p>I'm confused about the difference between the syntax used for associated types for protocols, on the one hand, and generic types on the other.</p>
<p>In Swift, for example, one can define a generic type using something like</p>
<pre><code>struct Stack<T> {
var items = [T]()
mutating func push(item: T) {
items.append(item)
}
mutating func pop() -> T {
return items.removeLast()
}
}
</code></pre>
<p>while one defines a protocol with associated types using something like</p>
<pre><code>protocol Container {
associatedtype T
mutating func append(item: T)
var count: Int { get }
subscript(i: Int) -> T { get }
}
</code></pre>
<p>Why isn't the latter just:</p>
<pre><code>protocol Container<T> {
mutating func append(item: T)
var count: Int { get }
subscript(i: Int) -> T { get }
}
</code></pre>
<p>Is there some deep (or perhaps just obvious and lost on me) reason that the language hasn't adopted the latter syntax?</p>
<div id='dv3'></div>
<br><div id='dv4'></div><h1>Solution</h1>
<div id='dv5'></div>
<p>This has been covered a few times on the devlist. The basic answer is that associated types are more flexible than type parameters. While you have a specific case here of one type parameter, it is quite possible to have several. For instance, Collections have an Element type, but also an Index type and a Generator type. If you specialized them entirely with type parameterization, you'd have to talk about things like <code>Array<String, Int, Generator<String>></code> or the like. (This would allow me to create arrays that were subscripted by something other than Int, which could be considered a feature, but also adds a lot of complexity.)</p>
<p>It's possible to skip all that (Java does), but then you have fewer ways that you can constrain your types. Java in fact is pretty limited in how it can constrain types. You can't have an arbitrary indexing type on your collections in Java. Scala extends the Java type system with associated types just like Swift. Associated types have been incredibly powerful in Scala. They are also a regular source of confusion and hair-tearing.</p>
<p>Whether this extra power is worth it is a completely different question, and only time will tell. But associated types definitely are more powerful than simple type parameterization.</p>
<div id='dv6'></div>
<br>
<br>
<div id='dv7'></div>
Answered By - <a href="https://stackoverflow.com/questions/26554987/why-dont-associated-types-for-protocols-use-generic-type-syntax-in-swift/26555177#26555177">Rob Napier</a>
<div id='dv8'></div>
Answer Checked By - <a href="https://www.phpfixing.com/p/contact-us.html">Senaida (PHPFixing Volunteer)</a> <br>
<div id='dv9'></div>
Philhttp://www.blogger.com/profile/04542702300179882994noreply@blogger.com0tag:blogger.com,1999:blog-2576027613025728432.post-85631777563715943252022-12-21T18:55:00.001-08:002022-12-21T18:55:27.809-08:00[FIXED] How to print a variable within an array using its index
<div id='dv1'></div>
<h1>Issue</h1>
<div id='dv2'></div>
<p>I am practicing with for in loops and having trouble with this.</p>
<pre><code>places = ["phuket", "athens", "doha"]
for places in range(5):
if places == 0:
print("thailand," + places 0 + "is a cool place")
else:
print("not thailand")
</code></pre>
<p>When I try this, I get a syntax error with 'places 0'. I want it to print thailand, phuket, is a cool place. But no matter how I seem to format places 0 (with the 0 in [], with it in ()) I just keep getting syntax errors.</p>
<div id='dv3'></div>
<br><div id='dv4'></div><h1>Solution</h1>
<div id='dv5'></div>
<p>If you use enumerate you can get the index of the for loop.
<strong>i</strong> will be <strong>0, 1, 2</strong> and <strong>place</strong> will be <strong>phuket, athens, doha</strong>.
And you can use different logic depends on what you want.</p>
<pre><code>places = ["phuket", "athens", "doha"]
for i,place in enumerate(places):
if i == 0:
print("thailand," + place + "is a cool place")
else:
print("not thailand")
</code></pre>
<p>You can understand more here - <a href="https://realpython.com/python-enumerate/" rel="nofollow noreferrer">https://realpython.com/python-enumerate/</a></p>
<div id='dv6'></div>
<br>
<br>
<div id='dv7'></div>
Answered By - <a href="https://stackoverflow.com/questions/70983426/how-to-print-a-variable-within-an-array-using-its-index/70983625#70983625">Andrej Korjakin</a>
<div id='dv8'></div>
Answer Checked By - <a href="https://www.phpfixing.com/p/contact-us.html">Marilyn (PHPFixing Volunteer)</a> <br>
<div id='dv9'></div>
Philhttp://www.blogger.com/profile/04542702300179882994noreply@blogger.com0tag:blogger.com,1999:blog-2576027613025728432.post-59810549108984560882022-12-21T18:20:00.001-08:002022-12-21T18:20:45.233-08:00[FIXED] What is the difference between '/' and '//' when used for division?
<div id='dv1'></div>
<h1>Issue</h1>
<div id='dv2'></div>
<p>Is there a benefit to using one over the other? In Python 2, they both seem to return the same results:</p>
<pre><code>>>> 6/3
2
>>> 6//3
2
</code></pre>
<div id='dv3'></div>
<br><div id='dv4'></div><h1>Solution</h1>
<div id='dv5'></div>
<p>In Python 3.x, <code>5 / 2</code> will return <code>2.5</code> and <code>5 // 2</code> will return <code>2</code>. The former is floating point division, and the latter is <em><strong>floor division</strong></em>, sometimes also called <em><strong>integer division</strong></em>.</p>
<p>In Python 2.2 or later in the 2.x line, there is no difference for integers unless you perform a <code>from __future__ import division</code>, which causes Python 2.x to adopt the 3.x behavior.</p>
<p>Regardless of the future import, <code>5.0 // 2</code> will return <code>2.0</code> since that's the floor division result of the operation.</p>
<p>You can find a detailed description at <em><a href="https://docs.python.org/whatsnew/2.2.html#pep-238-changing-the-division-operator" rel="noreferrer">PEP 238: Changing the Division Operator</a></em>.</p>
<div id='dv6'></div>
<br>
<br>
<div id='dv7'></div>
Answered By - <a href="https://stackoverflow.com/questions/183853/what-is-the-difference-between-and-when-used-for-division/183870#183870">Eli Courtwright</a>
<div id='dv8'></div>
Answer Checked By - <a href="https://www.phpfixing.com/p/contact-us.html">Katrina (PHPFixing Volunteer)</a> <br>
<div id='dv9'></div>
Philhttp://www.blogger.com/profile/04542702300179882994noreply@blogger.com0tag:blogger.com,1999:blog-2576027613025728432.post-73872660982890398442022-12-21T16:01:00.001-08:002022-12-21T16:01:03.737-08:00[FIXED] What is the equivalent of the Bash ` (backquote/backtick) in Windows PowerShell?
<div id='dv1'></div>
<h1>Issue</h1>
<div id='dv2'></div>
<p>I need the result of a command to be taken as a parameter in another command.</p>
<pre><code>command /opt <another command output>
</code></pre>
<p>On Bash Linux I would do</p>
<pre><code>command /opt `another command`
</code></pre>
<p>What is the equivalent of the ` symbol that works in Windows Terminal?</p>
<p>In particular, I am using the command</p>
<pre><code>'C:\Program Files\Display\display64.exe' /listdevices | findstr 22MP55 | %{$_ -replace "- .*",""}
</code></pre>
<p>where <code>findstr</code> is the windows equivalent to <code>grep</code> and the final command in the pipe is the equivalent to <code>sed</code>. The output of this command will be 1, 2 or 3, and I need this to be passed to the <code>%HERE</code> in the following line</p>
<pre><code>'C:\Program Files\Display\display64.exe' /device %HERE /rotate 90
</code></pre>
<p>The following minimal example, does not work:</p>
<pre><code>FOR /F %a in ('"C:\Program Files\Display\display64.exe" /listdevices | findstr 22MP55 | %{$_ -replace "- .*",""}') do echo "%a"
</code></pre>
<p>What I am doing wrong? (I am new in Windows).</p>
<div id='dv3'></div>
<br><div id='dv4'></div><h1>Solution</h1>
<div id='dv5'></div>
<p>Use <code>(...)</code>, the <a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Operators#grouping-operator--" rel="nofollow noreferrer">grouping operator</a> to pass output from a command as an argument to another command:</p>
<pre><code>'C:\Program Files\Display\display64.exe' /device (
'C:\Program Files\Display\display64.exe' /listdevices | findstr 22MP55 | %{$_ -replace "- .*",""}
) /rotate 90
</code></pre>
<p>Note: <code>$(...)</code>, the <a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Operators#subexpression-operator--" rel="nofollow noreferrer">subexpression operator</a>, works too, but <code>(...)</code> is usually sufficient and has no side effects; you only need <code>$(...)</code> for <em>multiple</em> (<code>;</code>-separated) statements - see <a href="https://stackoverflow.com/a/67695014/45375">this answer</a> for details.</p>
<hr />
<p>As for <strong>what you tried</strong>:</p>
<ul>
<li><p>In POSIX-compatible shells such as Bash, <code>`...`</code> is the legacy form of a <em>command substitution</em> whose modern syntax is <code>$(...)</code> - while PowerShell supports <code>$(...)</code> too, <code>(...)</code> is usually preferable, as noted.</p>
</li>
<li><p>In PowerShell, <code>`</code> serves as the <em>escape character</em> (only), analogous to <code>\</code> in POSIX-compatible shells - see the conceptual <a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Special_Characters" rel="nofollow noreferrer">about_Special_Characters</a> help topic.</p>
</li>
<li><p>Your attempts to use <code>FOR /F %a in ...</code> and <code>%HERE</code> (which should be <code>%HERE%</code>) relate to the <em>legacy <code>cmd.exe</code> shell</em> on Windows, not to its successor, PowerShell, whose syntax differs fundamentally.</p>
</li>
</ul>
<div id='dv6'></div>
<br>
<br>
<div id='dv7'></div>
Answered By - <a href="https://stackoverflow.com/questions/71022248/what-is-the-equivalent-of-the-bash-backquote-backtick-in-windows-powershell/71022399#71022399">mklement0</a>
<div id='dv8'></div>
Answer Checked By - <a href="https://www.phpfixing.com/p/contact-us.html">Dawn Plyler (PHPFixing Volunteer)</a> <br>
<div id='dv9'></div>
Philhttp://www.blogger.com/profile/04542702300179882994noreply@blogger.com0tag:blogger.com,1999:blog-2576027613025728432.post-2930300707176826162022-12-21T14:20:00.001-08:002022-12-21T14:20:54.020-08:00[FIXED] How do I modify a MySQL column to allow NULL?
<div id='dv1'></div>
<h1>Issue</h1>
<div id='dv2'></div>
<p>MySQL 5.0.45</p>
<p>What is the syntax to alter a table to allow a column to be null, alternately what's wrong with this:</p>
<pre><code>ALTER mytable MODIFY mycolumn varchar(255) null;
</code></pre>
<p>I interpreted the manual as just run the above and it would recreate the column, this time allowing null. The server is telling me I have syntactical errors. I just don't see them.</p>
<div id='dv3'></div>
<br><div id='dv4'></div><h1>Solution</h1>
<div id='dv5'></div>
<p>You want the following:</p>
<pre><code>ALTER TABLE mytable MODIFY mycolumn VARCHAR(255);
</code></pre>
<p>Columns are nullable by default. As long as the column is not declared <code>UNIQUE</code> or <code>NOT NULL</code>, there shouldn't be any problems.</p>
<div id='dv6'></div>
<br>
<br>
<div id='dv7'></div>
Answered By - <a href="https://stackoverflow.com/questions/212939/how-do-i-modify-a-mysql-column-to-allow-null/212947#212947">Daniel Spiewak</a>
<div id='dv8'></div>
Answer Checked By - <a href="https://www.phpfixing.com/p/contact-us.html">Marie Seifert (PHPFixing Admin)</a> <br>
<div id='dv9'></div>
Philhttp://www.blogger.com/profile/04542702300179882994noreply@blogger.com0