for (j = @array.length ; j > counter ; j = j-1) # counter is a variable
@array[j+1] = @array[j]
end
obviously this loop is invalid in ruby … can any one tell me something
equivalent to it ?
for (j = @array.length ; j > counter ; j = j-1) # counter is a variable
@array[j+1] = @array[j]
end
obviously this loop is invalid in ruby … can any one tell me something
equivalent to it ?
On Sep 4, 2011, at 9:46 AM, jack jones wrote:
for (j = @array.length ; j > counter ; j = j-1) # counter is a variable
@array[j+1] = @array[j]
endobviously this loop is invalid in ruby … can any one tell me something
equivalent to it ?
For this task, I would use #insert rather than iterating, as in
@array.insert(counter,@array[counter])
Dan N.
I didn’t know the insert method what are its arguements… are there
other way?
On Sun, Sep 4, 2011 at 7:19 AM, jack jones [email protected]
wrote:
I didn’t know the insert method what are its arguements…
You might be able to answer this as well as your original question by
reading the doc for Array. Just sayin’ …
thank you very much
On Sep 4, 2011, at 10:19 AM, jack jones wrote:
I didn’t know the insert method what are its arguements…
There are many excellent free online resources that do a much better
job of explaining it than I can. If you can’t find the answer after
doing some searching on your own, please feel free to contact me
offlist.
are there other way?
Yes, as with most tasks, there is many possible solutions.
Regards,
Dan N.
jack jones wrote in post #1020048:
for (j = @array.length ; j > counter ; j = j-1) # counter is a variable
@array[j+1] = @array[j]
endobviously this loop is invalid in ruby … can any one tell me something
equivalent to it ?
There is no element in an array at the index array.length, and there is
no element in an array at the index array.length + 1. So what language
is that code valid in?
On Sep 4, 2011, at 10:45 AM, jack jones wrote:
I’ve read about the method which inserts some element in a certain place
…
what I wanted to do is to shift all the elements to the back … not
inserting which will require rather complicated operation like deleting
the element after shifting it … I really don’t understand the line
you wrote, thank you
The #insert code, I think, did what your pseudo-code would have done.
But, given the above, I’m a bit confused as to what you are trying to
do.
Perhaps an example would be clearer. For instance, given:
@array = [ 0,1,2,3,4,5 ]
counter = 3
What do you want @array to look like after your operation?
Dan N.
I’ve read about the method which inserts some element in a certain place
…
what I wanted to do is to shift all the elements to the back … not
inserting which will require rather complicated operation like deleting
the element after shifting it … I really don’t understand the line
you wrote, thank you
jack jones wrote in post #1020055:
I’ve read about the method which inserts some element in a certain place
…
what I wanted to do is to shift all the elements to the back … not
inserting which will require rather complicated operation like deleting
the element after shifting it … I really don’t understand the line
you wrote, thank you
It’s pretty simple. It says, "Go to the position 2 in the array and
insert the value ‘red’.
array = [‘a’, ‘b’, ‘c’]
array.insert(1, 4_000)
p array
–output:–
[“a”, 4000, “b”, “c”]
On Sun, Sep 4, 2011 at 9:45 AM, jack jones [email protected]
wrote:
Insert nil. There are lots of options for docs.
rdoc.info:
http://rdoc.info/stdlib/core/1.9.2/Array#insert-instance_method
ruby-doc.org: class Array - RDoc Documentation
And you can’t have an array with a “gap” in it. ie what is returned if
you
access that index after you’ve shifted the elements? The obvious answer
is
nil, so just insert nil:
array = (0…10).to_a
counter = 5
array.insert counter, nil
array # => [0, 1, 2, 3, 4, nil, 5, 6, 7, 8, 9, 10]
On Mon, Sep 5, 2011 at 2:12 AM, Robert K.
[email protected]wrote:
irb(main):004:0> a=10.times.to_a
Kind regardsrobert
–
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
Insert takes multiple arguments, also.
a = *1…10
a # => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
index = 5
a.insert index, *a.pop(3)
a # => [1, 2, 3, 4, 5, 8, 9, 10, 6, 7]
On Sun, Sep 4, 2011 at 5:21 PM, Josh C. [email protected] wrote:
On Sun, Sep 4, 2011 at 9:45 AM, jack jones [email protected] wrote:
I’ve read about the method which inserts some element in a certain place
…
what I wanted to do is to shift all the elements to the back … not
inserting which will require rather complicated operation like deleting
the element after shifting it … I really don’t understand the line
you wrote, thank you
array.insert counter, nil
array # => [0, 1, 2, 3, 4, nil, 5, 6, 7, 8, 9, 10]
And if the length should stay the same an additional array.pop will
remove the last element.
Btw, for inserting multiple elements one can do:
irb(main):004:0> a=10.times.to_a
=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
irb(main):005:0> a[5,0] = Array.new 3
=> [nil, nil, nil]
irb(main):006:0> a.pop 3
=> [7, 8, 9]
irb(main):007:0> a
=> [0, 1, 2, 3, 4, nil, nil, nil, 5, 6]
irb(main):008:0> a.length
=> 10
Kind regards
robert
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs