Signup/Sign In

Answers

All questions must be answered. Here are the Answers given by this user in the Forum.

Using **array_search()** and **unset**, try the following:
***if (($key = array_search($del_val, $messages)) !== false) {
unset($messages[$key]);
}***
**array_search()** returns the key of the element it finds, which can be used to remove that element from the original array using **unset()**. It will return **FALSE** on failure, however it can return a false-y value on success (your key may be **0** for example), which is why the strict comparison **!==** operator is used.
The **if()** statement will check whether **array_search()** returned a value, and will only perform an action if it did.
4 years ago
**The tilde (~)**
In the simplest terms, the tilde (~) matches the most recent minor version (the middle number). ~1.2.3 will match all 1.2.x versions but will miss 1.3.0.
**The caret (^)**
The caret (^), on the other hand, is more relaxed. It will update you to the most recent major version (the first number). ^1.2.3 will match any 1.x.x release including 1.3.0, but will hold off on 2.0.0.
4 years ago
**npm list** for local packages or **npm list -g** for globally installed packages.

You can find the version of a specific package by passing its name as an argument. For example, **npm list grunt** will result in:

***projectName@projectVersion /path/to/project/folder
??? grunt@0.4.1***
Alternatively, you can just run **npm list** without passing a package name as an argument to see the versions of all your packages:
***
??? cli-color@0.1.6
? ??? es5-ext@0.7.1
??? coffee-script@1.3.3
??? less@1.3.0
??? sentry@0.1.2
? ??? file@0.2.1
? ??? underscore@1.3.3
??? uglify-js@1.2.6
***
4 years ago
Sure the code does work, but I'm pretty sure it doesn't do what you expect it to do. It just fires off multiple asynchronous calls, but the **printFiles** function does immediately return after that.
**Reading in sequence**
If you want to read the files in sequence, **you cannot use forEach** indeed. Just use a modern **for … of** loop instead, in which **await** will work as expected:

***async function printFiles () {
const files = await getFilePaths();

for (const file of files) {
const contents = await fs.readFile(file, 'utf8');
console.log(contents);
}
}***
**Reading in parallel**
If you want to read the files in parallel, **you cannot use forEach** indeed. Each of the **async** callback function calls does return a promise, but you're throwing them away instead of awaiting them. Just use **map** instead, and you can await the array of promises that you'll get with **Promise.all**:

***async function printFiles () {
const files = await getFilePaths();

await Promise.all(files.map(async (file) => {
const contents = await fs.readFile(file, 'utf8')
console.log(contents)
}));
}***
4 years ago
Apparently, there was a **/Users/myusername/local** folder that contained a **include** with **node** and **lib** with **node** and **node_modules**. How and why this was created instead of in my **/usr/local** folder, I do not know.
Deleting these local references fixed the phantom v0.6.1-pre. If anyone has an explanation, I'll choose that as the correct answer.
**EDIT**:
You may need to do the additional instructions as well:
***sudo rm -rf /usr/local/{lib/node{,/.npm,_modules},bin,share/man}/{npm*,node*,man1/node*}***
which is the equivalent of (same as above)...
***sudo rm -rf /usr/local/bin/npm /usr/local/share/man/man1/node* /usr/local/lib/dtrace/node.d ~/.npm ~/.node-gyp ***
or (same as above) broken down...
To completely uninstall node + npm is to do the following:
go to **/usr/local/lib** and delete any **node** and **node_modules**
go to **/usr/local/include** and delete any **node** and **node_modules** directory
if you installed with **brew install node**, then run **brew uninstall node** in your terminal
check your Home directory for any **local** or **lib** or **include** folders, and delete any **node** or **node_modules** from there
go to **/usr/local/bin** and delete any **node** executable
You may also need to do:
***sudo rm -rf /opt/local/bin/node /opt/local/include/node /opt/local/lib/node_modules
sudo rm -rf /usr/local/bin/npm /usr/local/share/man/man1/node.1 /usr/local/lib/dtrace/node.d***
Additionally, NVM modifies the PATH variable in **$HOME/.bashrc**, which must be **reverted manually**.
Then download **nvm** and follow the instructions to install node. The latest versions of node come with **npm**, I believe, but you can also reinstall that as well.
4 years ago
***import numpy as np
np.set_printoptions(threshold=np.inf)***
I suggest using **np.inf** instead of **np.nan** which is suggested by others. They both work for your purpose, but by setting the threshold to "infinity" it is obvious to everybody reading your code what you mean. Having a threshold of "not a number" seems a little vague to me.
4 years ago
**numpy.savetxt** saves an array to a text file.
***import numpy
a = numpy.asarray([ [1,2,3], [4,5,6], [7,8,9] ])
numpy.savetxt("foo.csv", a, delimiter=",")***
4 years ago
If one wants to create Java object from JSON and vice versa, use GSON or JACKSON third party jars etc.
***//from object to JSON
Gson gson = new Gson();
gson.toJson(yourObject);
// from JSON to object
yourObject o = gson.fromJson(JSONString,yourObject.class);***
But if one just want to parse a JSON string and get some values, (OR create a JSON string from scratch to send over wire) just use JaveEE jar which contains JsonReader, JsonArray, JsonObject etc. You may want to download the implementation of that spec like javax.json. With these two jars I am able to parse the json and use the values.
These APIs actually follow the DOM/SAX parsing model of XML.
***Response response = request.get(); // REST call
JsonReader jsonReader = Json.createReader(new StringReader(response.readEntity(String.class)));
JsonArray jsonArray = jsonReader.readArray();
ListIterator l = jsonArray.listIterator();
while ( l.hasNext() ) {
JsonObject j = (JsonObject)l.next();
JsonObject ciAttr = j.getJsonObject("ciAttributes");***
4 years ago
Try this
***String[] arr = list.toArray(new String[list.size()]);***
4 years ago
Since **Date** implements **Comparable**, it has a **compareTo** method just like **String** does.
So your custom **Comparator** could look like this:
***public class CustomComparator implements Comparator {
@Override
public int compare(MyObject o1, MyObject o2) {
return o1.getStartDate().compareTo(o2.getStartDate());
}
}***
The **compare()** method must return an **int**, so you couldn't directly return a **boolean** like you were planning to anyway.
Your sorting code would be just about like you wrote:
***Collections.sort(Database.arrayList, new CustomComparator());***
A slightly shorter way to write all this, if you don't need to reuse your comparator, is to write it as an inline anonymous class:
***Collections.sort(Database.arrayList, new Comparator() {
@Override
public int compare(MyObject o1, MyObject o2) {
return o1.getStartDate().compareTo(o2.getStartDate());
}
});***
*Since java-8*
You can now write the last example in a shorter form by using a lambda expression for the **Comparator**:
***Collections.sort(Database.arrayList,
(o1, o2) -> o1.getStartDate().compareTo(o2.getStartDate()));***
And **List** has a **sort(Comparator)** method, so you can shorten this even further:
***Database.arrayList.sort((o1, o2) -> o1.getStartDate().compareTo(o2.getStartDate()));***
This is such a common idiom that there's a built-in method to generate a **Comparator** for a class with a **Comparable** key:
***Database.arrayList.sort(Comparator.comparing(MyObject::getStartDate));***
All of these are equivalent forms.
4 years ago
One key difference not yet mentioned is that while sleeping a Thread does not release the locks it holds, while waiting releases the lock on the object that **wait()** is called on.
***synchronized(LOCK) {
Thread.sleep(1000); // LOCK is held
}***
***synchronized(LOCK) {
LOCK.wait(); // LOCK is not held
}***
4 years ago
This is one way.

***Map h = new HashMap() {{
put("a","b");
}};***
4 years ago