Neo4j access array of property to edit - arrays

Does anyone know how to access an array property of a node and change the value?
Is there some easy way in cypher like we do in C/C++/Java
array[index] = "some value"
I need to access an array element property of a node and change its value on specific conditions.

For reading the n-th element of an array you can simply use the subscript operator:
return [1,2,3,4,5,6][2]
gives you the third element of the array, 3 in this case.
If you want to replace e.g. the 4th element of an array with a value of 999, you might use
with [1,2,3,4,5,6] as myarray
return myarray[0..3] + 999 + myarray[4..length(myarray)]

Related

How I could replace and array element by array index in ruby?

I have an array like this I use
inputx.scan(/.*?\n/)
for create array this is a representation of my array
element 1 => [car;dog;soda]
element 2 => [bunny;pc;laptop]
element 3 => [hand;sword;shield]
this is my text file I use scan method for create array inputx.scan(/.*?\n/)
car;dog;soda
bunny;pc;laptop
hand;sword;shield
I need to replace each comma by number of array for obtain this
this is my expected output
in this output I replace ";" by "nthelementnumber;" example 1;
car1;dog1;soda
bunny2;pc2;laptop
hand3;sword3;shield
Please help me
It's a bit hard to tell what exactly your array looks like, but I'm going to take a guess:
element = ['car;dog;soda',
'bunny;pc;laptop',
'hand;sword;shield']
If that's correct, you can get the output you are looking for with something like:
element.each_index {|i| element[i] = element[i].gsub(';', "#{i+1};")}
The each_index iterator gives you each index (unsurprisingly). Then you can use each index to manipulate each value in the array.

Access 2d arrays in julia of my own type

I have created my own type:
type T
name
pos
end
What i want to do is create a 2d array of this type. This is how i did it:
arr = Array{T}(10,10) #create 2d Array
This creates the 2d array (10 by 10) with all the elements being undefined. (im thinking my problem is here)
So when i try to change just one element of the array
arr[1,1].name = "Hi"
I get this error:
ERROR: UndefRefError: access to undefined reference
What how I tried to fix the issue is by creating a default instance of T and iterate through the array and set every element equal to the default.
default = T("Hi",1)
for i = 1:10
for j = 1:10
arr[i,j] = default
end
end
Now, this sets every element in the array to the default value succesfully but now the problem is that if i try to change the value of just one element of the array, every element of the array is changed to that value.
arr[2,4].name ="Hello"
After that line all the elements have the value of "Hello".
Is that not how you are supposed to change values in an array? When i do the same on an Int64 2d array everything works how i would expect.
Everything is working as it should.
arr[i,j] = default
sets arr[i,j] to the reference default which is the instance of T("Hi",1). So every single arr[i,j] is referring to the same instance of the type.
arr[1,1].name = "Hi"
does not work because when you do arr = Array{T}(10,10) you create a 10x10 empty array with the ability of holding Ts, but you haven't put any Ts in there!
Thus what you want to be doing is:
arr[i,j] = T("Hi",1)
which will both make a T and make a[i,j] refer to it. Since each line is making a new T, they will refer to different instances and act separately.

Why "array.length" returns array object when we put it inside array[]?

rand(array.length) # returns random index <br>
array[rand(array.length)] # returns random array object
I can't understand the logic behind. I would assume that second example also returns random index and then store it in array.
kitty = [100,102,104,105]
rand(kitty.length) # returns index, for example 3 ( or 0,1,2 )
array[rand(kitty.length)] # returns random array object, for example 104 ( or 100,102,105)
While array.sample would be the best way to get a random element from an array, I believe OP is asking how the chaining of methods works.
When you call: rand(array.length) a number is returned, true. However in the case of:
array[ rand(array.length) ]
a number is returned but then fed immediately into the array call, which gives you the object at that array index.
Is this a tutorial? If so, don trust it. array.sample is how to do it.
ruby code
kitty = [100,102,104,105]
kitty.sample #to get random array elements
The behavior you are describing is expected:
array[index] is a reference to an object in the provided array where index is a numeric value that is less than array.length since rand(array.length) returns a random numeric value less than array.length you are effectively accessing an element at a random index of the given array.
The same behavior can be obtained with array.sample though and is recommended.
For more information on Ruby's arrays please see the documentation at: http://ruby-doc.org/core-2.2.0/Array.html

initializing a 2d array, each element is a list?

So, what I am trying to do is create a variable that holds 50 lists/arrays. Accessing an element in this variable would return one of the lists. If there are no elements at a given index, I would like it to return [].
My logic to initialize this would be something like:
spectrum_map=[];
for n=1:spectrum_blocks
spectrum_map=[spectrum_map,[]];
end
However, after doing so, I simply get an empty matrix:
spectrum_map =
[]
What I want to see is something like:
spectrum_map =
[] [] [] [] [] ....
That way, if I were to access spectrum_map(2), I would see that it is empty. However, instead I would get an error that the index exceeds matrix dimensions.
Is there some other way to do what I am trying to achieve?
You can get the effect you're looking for if you use a cell array instead of a matrix.
spectrum_map = cell( 1, 50 );
spectrum_map{50}
ans =
[]
If you need to convert back to a matrix later (to perform some math on it, for instance) you can use the cell2mat function.

Inserting data into an array sequentially

I am currently trying to figure out how to design some sort of loop to insert data into an array sequentially. I'm using Javascript in the Unity3D engine.
Basically, I want to store a bunch of coordinate locations in an array. Whenever the user clicks the screen, my script will grab the coordinate location. The problem is, I'm unsure about how to insert this into an array.
How would I check the array's index to make sure if array[0] is taken, then use array[1]? Maybe some sort of For loop or counter?
Thanks
To just add onto the end of an array, just use .push().
var myArray = [];
var coord1 = [12,59];
var coord2 = [87,23];
myArray.push(coord1);
myArray.push(coord2);
myArray, now contains two items (each which is an array of two coordinates).
Now, you wouldn't do it this way if you were just statically declaring everything as I've done here (you could just statically declare the whole array), but I just whipped up this sample to show you how push works to add an item onto the end of an array.
See https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/push for some reference doc on push.
In case you need to know the array's length when reading the array in the future, you can use the .length attribute.
var lengthOfArray = myArray.length;
Using the .push() method as suggested by jfriend00 is my recommendation too, but to answer your question about how to work out what the next index is you can use the array's length property. Because JavaScript arrays are zero-based The length property will return an integer one higher than the current highest index, so length will also be the index value to use if you want to add another item at the end:
anArray[anArray.length] = someValue; // add to end of array
To get the last element in the array you of course say anArray[anArray.length-1].
Note that for most purposes length will give the number of elements in the array, but I said "one higher than the current highest index" above because JavaScript arrays are quite happy for you to skip indexes:
var myArray = [];
myArray[0] = "something";
myArray[1] = "something else";
myArray[50] = "something else again";
alert(myArray.length); // gives '51'
// accessing any unused indexes will return undefined

Resources