0

I have an array filled with several dates

 arry = ["06-01-2014", "07-04-2014", "14-01-2014"
         "14-04-2014", "16-01-2014" "27-03-2014",
         "30-12-2013", "31-03-2014", "27-02-2014"]

I would like to remove element 6 and up Preferably have 2 different arrays.

Is that possible?

3
  • 3
    are you looking for arry[0..5] and arry[6..-1]? Commented Apr 24, 2014 at 19:05
  • arry[5..-1] works great. thanks add it as an answer so I can give you credit. Commented Apr 24, 2014 at 19:10
  • Please add two commas to arry. No reply req'd, as I'll delete this after you've done the edit. You should always run the code you post to make sure it's working. Commented Apr 24, 2014 at 19:59

3 Answers 3

3

Feel free to accept @JanDvorak's answer but you could do this as well

arry = ["06-01-2014", "07-04-2014", "14-01-2014","14-04-2014", "16-01-2014", "27-03-2014","30-12-2013", "31-03-2014", "27-02-2014"]
first_five = arry.shift(5)
#=> ["06-01-2014", "07-04-2014", "14-01-2014", "14-04-2014", "16-01-2014"]
arry 
#=> ["27-03-2014","30-12-2013", "31-03-2014", "27-02-2014"]

or non-destructively (maintaining arry)

arry = ["06-01-2014", "07-04-2014", "14-01-2014","14-04-2014", "16-01-2014", "27-03-2014","30-12-2013", "31-03-2014", "27-02-2014"]
first_five,rest = arry.partition.with_index{|a,i| i < 5}
first_five
#=> ["06-01-2014", "07-04-2014", "14-01-2014", "14-04-2014", "16-01-2014"]
rest
#=> ["27-03-2014","30-12-2013", "31-03-2014", "27-02-2014"]
arry
#=> ["06-01-2014", "07-04-2014", "14-01-2014", "14-04-2014", "16-01-2014","27-03-2014", "30-12-2013", "31-03-2014", "27-02-2014"]
Sign up to request clarification or add additional context in comments.

Comments

1

Another way is to delete using index

arry.delete_at(5)

and as @Jan Dvorak mentioned you can split them based on index positions using

arry[0..5] and arry[5..-1]

2 Comments

What's the point of deleting before slicing? All it does is change arry, which may not be desired.
delete_at(index) will remove a single element at that index you could use delete_if.with_index and choose what indexes to delete in a block
0

Here's another (non-destructive) way, but nothing could be simpler and clearer than what @Jan suggested.

arr = ["06-01-2014", "07-04-2014", "14-01-2014",
       "14-04-2014", "16-01-2014", "27-03-2014",
       "30-12-2013", "31-03-2014", "27-02-2014"]

[arr.first(5), arr.drop(5)]
  #=> [["06-01-2014", "07-04-2014", "14-01-2014", "14-04-2014", "16-01-2014"],
  #    ["27-03-2014", "30-12-2013", "31-03-2014", "27-02-2014"]]

3 Comments

not that it matters I just think it looks reads nicer to use take instead of first when used with drop also it should be [arr.first(5),arr.drop(5)] otherwise you are losing an element.
@engineersmnky, thanks for the correction. (I had misread the question as, in effect, partition on the 6th element.) As for take instead of first, I'm indifferent. You could argue that the word take suggests remove (but we could say the same about drop), whereas first does not.
I agree but the same argument could be made for drop either way I love to see how many ways you can skin a cat in ruby :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.