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

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.

Related

How to show elements of array?

I have a small problem. I created a large array.
It looks like this:
var Array = [
["text10", "text11", ["text01", "text02"]],
["text20", "text21", ["text11", "text12"]]
]
If we write this way: Array[0] that shows all the elements.
If we write this way: Array[0][0] that shows "text1".
If we write this way: Array[0][2] that shows
-2 elements
-- 0: "text01"
-- 1: "text02"
.
If we write this way: Array[0][2].count or Array[0][2][0] it will not work
How do I choose each item, I need these elements for the tableView
The problem basically is that your inner array is illegal. Swift arrays must consist of elements of a single type. You have two types of element, String and Array Of String. Swift tries to compensate but the result is that double indexing can’t work, not least because there is no way to know whether a particular element will have a String or an Array in it.
The solution is to rearchitect completely. If your array entries all consist of the same pattern String plus String plus Array of String, then the pattern tells you what to do; that should be a custom struct, not an array at all.
as #matt already answered but I want to add this thing
Why Array[0][2].count or Array[0][2][0] not work
If you Define array
var array = [
["text10", "text11", ["text01", "text02"]],
["text20", "text21", ["text11", "text12"]]
]
And when you type array you can see it's type is [[Any]] However it contains String as well as Array
So When you try to get Array[0][2] Swift does not know that your array at position 2 has another array which can have count
EDIT
What you are asking now is Array of dictionary I suggest you to go with model i.e create struct or class and use it instead of dictionary
Now If you want to create dictionary then
var arrOfDict = ["text10" : ["text01", "text02"] , "text11" : ["text11", "text12"]]
And you can access with key name let arrayatZero = arrOfDict["text10"] as? [String]

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

Neo4j access array of property to edit

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)]

push ELEMENTS of array to an array using Perl

using Perl I am trying to push the elements of an array to another array, and not the whole array. But I'm not getting my goal.
I tried this:
push #tmp_entities_all, #tmp_entities;
But I got the whole small array as an element in the bigger array.
Then I tried it with a loop:
for (#tmp_entities) {push #tmp_entities_all, $_;}
But the same results, the whole #tmp_entities appears as an element, and that what I dont want.
I need one dimension in the array and not array of arrays!! Should I cast something before pushing? or what is the problem?
Thanx a lot.
Out of your comments, its obvious, that #tmp_entities contains only one element, that is an array reference to the elements that you expected to be elements of #tmp_entities.
So you perhaps declared your array with an array refence instead of using a set of elements.
The line
push #tmp_entities_all, #tmp_entities;
definitly works for a normal array.
In your case, your could try ...
push #tmp_entities_all, $tmp_entities[0];
or you simply try to initialize your array with its value like
my #tmp_entities = ( 1, 2, 3 ); # initialize array with 3 elements of type int
instead of
my #tmp_entities = [ 1, 2, 3 ]; # initialize array with 1 element that is an array reference with 3 elements of type int
I know, that this is the case, because this is why your for-loop sample works with #$_ ;D (it is equivalent to push #tmp_entities_all, $tmp_entities[0]; in this situation).

AS3 How to remove object from array

I have a array called 'BODY_STAT_ARRAY' and in this array it holds a object called 'body_stat'. This array needs to increase a decrease in size all the time, how do i remove the last object from the array, here is a example which doesn't work
if(BODY_STAT_ARRAY.length > target_size)
{
BODY_STAT_ARRAY.slice(BODY_STAT_ARRAY.indexOf(BODY_STAT_ARRAY[BODY_STAT_ARRAY.length-1]),1)
// this line above should remove the last object in the array
}
So where am I going wrong, how do you make it work.
If you can help, I would love to find out.
The easiest way to remove the last object from an array is to call pop:
BODY_STAT_ARRAY.pop();
Slice will not modify the Original Array . It will give an another Array with the defined range .
Only Splice will modify the original Array , so as to remove or add elements .
var a:Array = [1,2,3,4];
a.slice(1,1);
trace(a); // Output is : 1,2,3,4
a.splice(1,1);
trace(a); // Output is : 1,3,4

Resources