I have a array of n elements say
snaps = ["s-1","s-2","s-3","s-4",..."s-n-2","s-n-1","s-n"]
now I want to create two diff array such that one array contains last 5 elements and another contains remaining elements.For example
snap1 = ["s-1","s-2","s-3","s-4",...]
snap2 = ["s-n-5","s-n-3","s-n-2","s-n-1","s-n"]
How can I do this.
snap1 = snaps.dup
snap2 = snap1.pop(5)
snap2 = snaps[-5, 5]
Or
snap2 = snaps.last(5) # As suggested my BroiSatse
will give you an array with the last 5 elements
For the remaining, you can do
snap1 = snaps[0..-6]
You can use slice! to create the two arrays:
snaps = ["s-1","s-2","s-3","s-4","s-n-5","s-n-3","s-n-2","s-n-1","s-n"]
snap2 = snaps.slice!(-5..-1)
# => ["s-n-5", "s-n-3", "s-n-2", "s-n-1", "s-n"]
snaps
# => ["s-1", "s-2", "s-3", "s-4"]
Related
I have some arrays that I put into one array called alist[]. I am iterating over the array to print out all values of alist[]. I need to find a value at alist[2][i] and then remove the alist[0][i], alist[1][i], alist[2][i], alist[3][i], alist[4][i] from my array alist[][]. (I removed the code that fills my arrays so it is easier to read my question)
This is my best guess below but it is not working. Would anyone have any ideas?
#declare arrays
nsymbol = []
sname = []
etf = []
testv = []
financials = []
alist = []
#create one array with all other arrays
alist.push(nsymbol, sname, etf, testv, financials)
(0...nsymbol.length).each do |i|
(0...alist.length).each do |j|
if (alist[2][i] || '').include? 'Y'
alist.delete_at(0)
alist.delete_at(1)
alist.delete_at(2)
alist.delete_at(3)
alist.delete_at(4)
end
#print whole array out
puts alist[j][i]
end
end
By performing alist.delete_at(0) you delete the first item of alist so to speak alist[0][0..N] but you want to delete alist[0][i] so you need to delete the ith item of alist[0].
alist[0].delete_at(i)
alist[1].delete_at(i)
# etc.
Because you are printing your array just after deleting the new contents it doesn't matter, but if you want to use the array after this you should break the loop after deleting the entries, because deleting the entries leads to another entry now being the item alist[2][i] and eventually to a further deletion of the entries. (Though this might also be exactly what you want).
I have an array, and want to copy it so I can check if it has changed.
The array looks like this:
#table = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]
and I copy the values like this:
#old_table = #table.clone
I have two sorting methods, one that sorts it hortizontaly and the other sorts it vertically.
Everything works fine with the horizontal method but when I use the vertical routine, it changes the value of #old_table to the cloned array.
I already checked the object id and it's not the same. I tried with other ways to copy the value too but I get the same result.
Horizontal:
currline = 0
4.times do
#line = #table[currline].clone.reverse
compare
sort
#table[currline] = #line.reverse
currline += 1
end
Vertical:
currline = 0
4.times do
#line = [#table[0][currline],#table[1][currline],#table[2][currline],#table[3][currline]].reverse
compare
sort
#line.reverse!
#table[0][currline] = #line[0]
#table[1][currline] = #line[1]
#table[2][currline] = #line[2]
#table[3][currline] = #line[3]
currline += 1
end
Here's a link to the whole code: http://pastebin.com/1xzLx5ib
I need help to figure out why the vertical method changes the value of #old_table to the original when it shouldn't.
It's because the outer array is cloned, but not the 4 inner arrays.
#table = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]
#old_table = #table.clone
#old_table.object_id
# => 70198498995020
#table.object_id
# => 70198498975440 (So far so good)
#old_table[0].object_id
# => 70198498975520
#table[0].object_id
# => 70198498975520 (Same row id!)
One simple way to fix this is to serialize and unserialize the array:
#old_table = Marshal.load Marshal.dump(#table)
I have a hash
h = {}
h.compare_by_identity
h[2.51] = 1
h1[2.51] = 2
Edit: h1[2.51] = 2 should be h[2.51] = 2
it is ok with duplicate key. But when i use
Hash[h.sort]
it return only one value with key like
{2.51=>2}
is there any way to get the two values from the hash in sorted order?
Starting with ruby version 2.0 the key 2.51 is actually the same object (because of ruby internal caching) in both assignments. Try to output 2.51.object_id for both cases and it will output the same id.
Since it can't be done with floats, turn them into strings:
h = {}
h.compare_by_identity
h[2.51.to_s] = 2
h[2.51.to_s] = 1
p h.sort # => [["2.51", 1], ["2.51", 2]]
I have a function toWords which converts a integer into a word
e.g. toWords(500, tableWords) gives fivehundred
I have an array of numbers h = (1..999).to_a, and I want to go through this array and convert each number into a word and store it in a new array. My current attempt to do this is:
h = (1..999).to_a
Lh = h.each do |i| toWords(i, tableWords) end
However, the contents of Lh is simply the integers from 1 to 999 and not the output of my toWords function. How do I do this? I'm thinking of something along the lines of sapply in R.
Even better is if my new array Lh can have two columns, the first column containing the integers in number format, and the second column would be the corresponding number in words.
Thank you!
To get your two columns, you can do the following
(1..999).map {|x| [x, toWords(x, tableWords)]}
As per Cicada's comment, the answer is:
Lh = h.map{|x| toWords(x, tableWords)}
I declared my multidimentional array like this:
Dim invoice_discountitems(100, 1) As String
I Fill my array with this:
'Fill Array with items discounts
For i As Int16 = 0 To data_set.Tables("discount_items").Rows.Count - 1
invoice_discountitems(i, 0) = data_set.Tables("discount_items").Rows(i).Item("item_code")
invoice_discountitems(i, 1) = data_set.Tables("discount_items").Rows(i).Item("discountitem_average")
Next
Now how i can remove the filled items of this array?
Thanks in Advance
Because an array is statically sized, an empty array is the same as a freshly initialized array. So, if you want to clear the whole thing:
invoice_discountitems = New String(100, 1)
Or, if you wish to clear specific elements, use Array.Clear()
Array.Clear(invoice_discountitems, 1, 10)