[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

array question

Li Chen

11/21/2006 4:30:00 AM

Hi all,

I want to build a new array from an old one with every element being
duplicated except the first and last element. And here are my codes. I
wonder if this is a real Ruby way to do it.

Thank you for your input.

Li


#########
C:\>irb
irb(main):001:0> array=[1,2,3,4,5,6,7,8,9,10]
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
irb(main):002:0> array_new=Array.new
=> []
irb(main):003:0> array.each do |e|
irb(main):004:1* array_new<<"#{e}"
irb(main):005:1> array_new<<"#{e}"
irb(main):006:1> end
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
irb(main):007:0>
irb(main):008:0* array_new.delete_at(0)
=> "1"
irb(main):009:0> array_new.delete_at(array_new.size-1)
=> "10"
irb(main):010:0> puts array_new
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
9
10
=> nil


--
Posted via http://www.ruby-....

32 Answers

Wilson Bilkovich

11/21/2006 4:38:00 AM

0

On 11/20/06, Li Chen <chen_li3@yahoo.com> wrote:
> Hi all,
>
> I want to build a new array from an old one with every element being
> duplicated except the first and last element. And here are my codes. I
> wonder if this is a real Ruby way to do it.
>

A couple of ways: (there are probably dozens more)
# given:
array = [1,2,3,4,5,6]

# 1.
new_array = array[1..-2]

# 2.
new_array = array.dup
new_array.shift # shifts off the first element
new_array.pop # pops off the last element

#1 uses Array#[], which is a synonym for Array#slice.
1..-2 is a Range parameter. In this case, it says you want a slice of
the array containing everything but the first and last element.

dblack

11/21/2006 4:46:00 AM

0

dblack

11/21/2006 4:47:00 AM

0

dblack

11/21/2006 4:50:00 AM

0

Li Chen

11/21/2006 5:13:00 AM

0


> A couple of ways: (there are probably dozens more)
> # given:
> array = [1,2,3,4,5,6]
>
> # 1.
> new_array = array[1..-2]
>
> # 2.
> new_array = array.dup
> new_array.shift # shifts off the first element
> new_array.pop # pops off the last element
>
> #1 uses Array#[], which is a synonym for Array#slice.
> 1..-2 is a Range parameter. In this case, it says you want a slice of
> the array containing everything but the first and last element.


Hi,

I run array=[1,2,3,4,5]
new_array = array.dup
new_array.shift
new_array.pop
I get [2,3,4] but I want this result [1,2,2,3,3,4,4,5]

Li

--
Posted via http://www.ruby-....

Wilson Bilkovich

11/21/2006 5:18:00 AM

0

On 11/21/06, Li Chen <chen_li3@yahoo.com> wrote:
>
> > A couple of ways: (there are probably dozens more)
> > # given:
> > array = [1,2,3,4,5,6]
> >
> > # 1.
> > new_array = array[1..-2]
> >
> > # 2.
> > new_array = array.dup
> > new_array.shift # shifts off the first element
> > new_array.pop # pops off the last element
> >
> > #1 uses Array#[], which is a synonym for Array#slice.
> > 1..-2 is a Range parameter. In this case, it says you want a slice of
> > the array containing everything but the first and last element.
>
>
> Hi,
>
> I run array=[1,2,3,4,5]
> new_array = array.dup
> new_array.shift
> new_array.pop
> I get [2,3,4] but I want this result [1,2,2,3,3,4,4,5]
>
> Li

Yeah. Sorry. I didn't read your question clearly enough. I apologize
for confusing the issue. Check out the other replies from people with
better reading comprehension late at night.
Heh.

J. B. Rainsberger

11/21/2006 6:04:00 AM

0

Daniel N wrote:
> On 11/21/06, Wilson Bilkovich <wilsonb@gmail.com> wrote:
>>
>>
>> >
>>
>> A couple of ways: (there are probably dozens more)
>> # given:
>> array = [1,2,3,4,5,6]
>>
>> # 1.
>> new_array = array[1..-2]
>
>
> This would not duplicate the inner elements though.
>
> To build on this one.
> b = a.inject( [] ) { |a,e| a << [e,e]}.flatten[1..-2]
>
> This looks pretty jarring to my eyes though :(

I would use map over inject into []:

b = a.map { |each| [each,each] }.flatten[1..-2]

Either that, or recognize that it's equivalent duplicating all except
first and last.

b = [a[0], a[1..-2].map { |each| [each, each] }.flatten, a[-1]]

In either case, we can avoid the flatten entirely, at the expense of
returning to inject:

b = a.inject([]) {|sum, each| sum.concat([each, each])[1..-2]
or
b = [a[0],
a[1..-2].inject([]) {|sum, each| sum.concat([each, each])},
a[-1]]

Of course, if we know the items in the array are in ascending order, we
can /really/ cheat:

b = (a*2).sort[1..-2]

That's as many as I could think of.
--
J. B. (Joe) Rainsberger :: http://www....
Your guide to software craftsmanship
JUnit Recipes: Practical Methods for Programmer Testing
2005 Gordon Pask Award for contribution Agile Software Practice

Li Chen

11/21/2006 7:06:00 AM

0

J. B. Rainsberger wrote:

> Of course, if we know the items in the array are in ascending order, we
> can /really/ cheat:
>
> b = (a*2).sort[1..-2]
>
> That's as many as I could think of.

Thank you but elements in the array are in random order.

Li


--
Posted via http://www.ruby-....

William James

11/21/2006 7:19:00 AM

0


Li Chen wrote:
> Hi all,
>
> I want to build a new array from an old one with every element being
> duplicated except the first and last element. And here are my codes. I
> wonder if this is a real Ruby way to do it.

>> array=[1,2,3,4,5,6,7,8,9,10]
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>> array.zip(array).flatten[1..-2]
=> [1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10]

Robert Klemme

11/21/2006 8:30:00 AM

0

On 21.11.2006 08:18, William James wrote:
> Li Chen wrote:
>> Hi all,
>>
>> I want to build a new array from an old one with every element being
>> duplicated except the first and last element. And here are my codes. I
>> wonder if this is a real Ruby way to do it.
>
>>> array=[1,2,3,4,5,6,7,8,9,10]
> => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> array.zip(array).flatten[1..-2]
> => [1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10]

That's cute! I have another one with #inject (of course):

irb(main):007:0> arr=(1..10).to_a
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
irb(main):008:0> copy=[]
=> []
irb(main):009:0> arr.inject{|a,b| copy << a << b; b}
=> 10
irb(main):010:0> copy
=> [1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10]

Kind regards

robert