[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Newbie - several array questions

Andy Elvey

2/18/2009 8:47:00 AM

Hi all - I'm a first-timer and very much love Ruby!

I have several questions involving arrays. First, I'll create a couple
of arrays to use in my examples -

arr1 = [1, 2, 3, 4, 5]
arr2 = ["foo", "bar", "baz", "abc", "def"]

What I want to do is the following -
a) I want to create another array that alternates the elements of these
two arrays, like this -
[1, "foo", 2, "bar", 3, "baz", 4, "abc", 5, "def]

b) In order to be able to create text tables, I'd like a function that
can take a character (like "|") and can apply it to an array so that you
end up with another array like this -
arr4 = ["|", "foo", "|", "bar", "|", "baz", "|"]

c) Finally, I'd like to create another function to do a very similar
thing to (b), but only have the character *between* the elements, like
this -
arr5 = ["foo", "|", "bar", "|", "baz"]

Very many thanks in advance for your help! (Many thanks to Matz too
for a great language! :) )
- latte
--
Posted via http://www.ruby-....

8 Answers

7stud --

2/18/2009 9:33:00 AM

0

Andy Elvey wrote:
>

arr1 = [1, 2, 3, 4, 5]
arr2 = ["foo", "bar", "baz", "abc", "def"]

resultA = arr1.zip(arr2).flatten

resultB = []
arr2.each do |item|
resultB << "|" << item
end
resultB << "|"

resultC = []
arr2.each do |item|
resultC << item << "|"
end
resultC.pop()

p resultA
p resultB
p resultC


--output:--
[1, "foo", 2, "bar", 3, "baz", 4, "abc", 5, "def"]
["|", "foo", "|", "bar", "|", "baz", "|", "abc", "|", "def", "|"]
["foo", "|", "bar", "|", "baz", "|", "abc", "|", "def"]


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

Peña, Botp

2/18/2009 9:37:00 AM

0

RnJvbTogQW5keSBFbHZleSBbbWFpbHRvOmFuZHkuZWx2ZXlAcGFyYWRpc2UubmV0Lm56XSANCiMg
YXJyMSA9IFsxLCAyLCAzLCA0LCA1XQ0KIyBhcnIyID0gWyJmb28iLCAiYmFyIiwgImJheiIsICJh
YmMiLCAiZGVmIl0NCg0KdHJ5ICN6aXANCg0KZWcsDQoNCg0KPiBhcnIxLnppcChhcnIyKS5mbGF0
dGVuDQo9PiBbMSwgImZvbyIsIDIsICJiYXIiLCAzLCAiYmF6IiwgNCwgImFiYyIsIDUsICJkZWYi
XQ0KDQo+IEFycmF5Lm5ldyhhcnIyLnNpemUrMSwifCIpLnppcChhcnIyKS5mbGF0dGVuWzAuLi0y
XQ0KPT4gWyJ8IiwgImZvbyIsICJ8IiwgImJhciIsICJ8IiwgImJheiIsICJ8IiwgImFiYyIsICJ8
IiwgImRlZiIsICJ8Il0NCg0KPiBhcnIyLnppcChBcnJheS5uZXcoYXJyMi5zaXplLTEsInwiKSku
ZmxhdHRlblswLi4tMl0NCj0+IFsiZm9vIiwgInwiLCAiYmFyIiwgInwiLCAiYmF6IiwgInwiLCAi
YWJjIiwgInwiLCAiZGVmIl0NCg==

7stud --

2/18/2009 9:48:00 AM

0

7stud -- wrote:
> arr1 = [1, 2, 3, 4, 5]
> arr2 = ["foo", "bar", "baz", "abc", "def"]
>
> resultA = arr1.zip(arr2).flatten
>

This is probably more efficient:

resultA = []
arr1.zip(arr2) do |item_arr1, item_arr2|
resultA << item_arr1 << item_arr2
end

Just remember: a bunch of methods chained together on one line is rarely
very efficient.

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

Heesob Park

2/18/2009 10:20:00 AM

0

2009/2/18 7stud -- <bbxx789_05ss@yahoo.com>:
> Andy Elvey wrote:
>>
>
> arr1 = [1, 2, 3, 4, 5]
> arr2 = ["foo", "bar", "baz", "abc", "def"]
>
> resultA = arr1.zip(arr2).flatten
>
> resultB = []
> arr2.each do |item|
> resultB << "|" << item
> end
> resultB << "|"
>
You can also write it using inject like this:
arr = arr2.inject(["|"]){|r,e|r<<e<<"|"}

> resultC = []
> arr2.each do |item|
> resultC << item << "|"
> end
> resultC.pop()
>
arr = arr2.inject([]){|r,e|r<<e<<"I"}
arr.pop

Regards,

Park Heesob

7stud --

2/18/2009 3:09:00 PM

0

Heesob Park wrote:
> 2009/2/18 7stud -- <bbxx789_05ss@yahoo.com>:
>> resultB << "|" << item
>> end
>> resultB << "|"
>>
> You can also write it using inject like this:
> arr = arr2.inject(["|"]){|r,e|r<<e<<"|"}
>

As long as you realize that inject() is always the most inefficient
solution possible.
--
Posted via http://www.ruby-....

Choi, Junegunn

2/18/2009 5:11:00 PM

0

>
> As long as you realize that inject() is always the most inefficient
> solution possible.
>

Can you elaborate on your claim? How inefficient, like, by orders of
magnitude? Is it serious enough to affect the overall performance of a
typical ruby application? In a situation like this, I'd prefer the
code which uses 'inject', since it shows the intention of the
programmer more clearly and concisely, while the performance penalty -
10% in my naive experiment - is irrelevant in this case. I agree that
we should be aware of the performance issues even when we're dealing
with a high-level language such as ruby, but your bold statement makes
me feel that you're trying to 'defend' your own code against the
others in some kind of a code contest.


--
junegunn.

Rick DeNatale

2/18/2009 6:32:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

On Wed, Feb 18, 2009 at 12:10 PM, Choi, Junegunn <jg@samdorr.net> wrote:

> >
> > As long as you realize that inject() is always the most inefficient
> > solution possible.
> >
>
> Can you elaborate on your claim? How inefficient, like, by orders of
> magnitude? Is it serious enough to affect the overall performance of a
> typical ruby application? In a situation like this, I'd prefer the
> code which uses 'inject', since it shows the intention of the
> programmer more clearly and concisely, while the performance penalty -
> 10% in my naive experiment - is irrelevant in this case. I agree that
> we should be aware of the performance issues even when we're dealing
> with a high-level language such as ruby, but your bold statement makes
> me feel that you're trying to 'defend' your own code against the
> others in some kind of a code contest.
>

I think that the statement that "inject() is always the inefficient solution
possible", even if it were true, which I'm not at all sure it is, isn't
particularly good advice.

One of my buddy Kent Beck's more well known memes is "Make it run. Make it
right, Make it fast." [1]. Although the roots of this advice can be traced
back to Butler Lampson [2]. This advice is something that anyone who wants
to be a software craftsman needs to take to heart.

The order is important. First you make sure that the code runs, then you
need to make it right. Making it right doesn't mean that it does the right
thing, that's a large part of making it run, it mean's making the code clear
and maintainable, either by you sometime in the future, or by someone else.
Only then you should make it fast, without unnecessarily harming keeping it
right. And the best way to do this is to slavishly follow the 3 rules of
optimization [3]

While there are some who just seem to hate inject, it can, as you point out,
be much more intention revealing, which helps in making code right.

This is just one of those things, like when and when not to use optional
parentheses in Ruby which need a bit of thoughtful reflection, rather than
blind adherence to convention.

[1] http://c2.com/cgi/wiki?MakeItWorkMakeItRight...
[2]
http://research.microsoft.com/en-us/um/people/blampson/33-hints/we...
[3] http://c2.com/cgi/wiki?RulesOfOp...

--
Rick DeNatale

Blog: http://talklikeaduck.denh...
Twitter: http://twitter.com/Ri...
WWR: http://www.workingwithrails.com/person/9021-ric...
LinkedIn: http://www.linkedin.com/in/ri...

Andy Elvey

2/19/2009 6:21:00 AM

0

Rick Denatale wrote:
> On Wed, Feb 18, 2009 at 12:10 PM, Choi, Junegunn <jg@samdorr.net> wrote:
>
>> 10% in my naive experiment - is irrelevant in this case. I agree that
>> we should be aware of the performance issues even when we're dealing
>> with a high-level language such as ruby, but your bold statement makes
>> me feel that you're trying to 'defend' your own code against the
>> others in some kind of a code contest.
>>
>
> I think that the statement that "inject() is always the inefficient
> solution possible", even if it were true, which I'm not at all sure it is, isn't particularly good advice.
>
> One of my buddy Kent Beck's more well known memes is "Make it run. Make
> it
> right, Make it fast." [1]. Although the roots of this advice can be
> traced
> back to Butler Lampson [2]. This advice is something that anyone who
> wants
> to be a software craftsman needs to take to heart.

( snip )

Great! Oh, and please forgive me for snipping a few lines here, Rick -
I got an automated message telling me to reduce the number of quoted
lines.

Thanks Rick, and a very big **thank you** to everyone else who has
replied here! It's great to be part of such an active, friendly and
generous community!

Bye for now - :-)
- latte
--
Posted via http://www.ruby-....