Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.ruby > #2474 > unrolled thread
| Started by | Cyril Joe <cyril_jose@ymail.com> |
|---|---|
| First post | 2011-04-07 11:38 -0500 |
| Last post | 2011-04-07 18:29 -0500 |
| Articles | 7 — 5 participants |
Back to article view | Back to comp.lang.ruby
newlines in array problem Cyril Joe <cyril_jose@ymail.com> - 2011-04-07 11:38 -0500
Re: newlines in array problem Michel Demazure <michel@demazure.com> - 2011-04-07 11:43 -0500
Re: newlines in array problem Jesús Gabriel y Galán <jgabrielygalan@gmail.com> - 2011-04-07 11:50 -0500
Re: newlines in array problem Cyril Jose <cyril_jose@ymail.com> - 2011-04-07 13:08 -0500
Re: newlines in array problem 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-07 13:34 -0500
Re: newlines in array problem Jesús Gabriel y Galán <jgabrielygalan@gmail.com> - 2011-04-07 14:17 -0500
Re: newlines in array problem 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-07 18:29 -0500
| From | Cyril Joe <cyril_jose@ymail.com> |
|---|---|
| Date | 2011-04-07 11:38 -0500 |
| Subject | newlines in array problem |
| Message-ID | <030a5c75c68358523471052f22f04f7b@ruby-forum.com> |
Is there a way to get rid of newlines in an array?
Example: array = ["hel\nlo", "bl\nah"]
I want the output to be new_array = ["hello", "blah"]
I tried:
array.each do |el|
el.delete("\n")
end
But that didn't work. Can't figure this out. Any suggestions?
--
Posted via http://www.ruby-forum.com/.
[toc] | [next] | [standalone]
| From | Michel Demazure <michel@demazure.com> |
|---|---|
| Date | 2011-04-07 11:43 -0500 |
| Message-ID | <f8ce27a394ded2b72095740d8c096987@ruby-forum.com> |
| In reply to | #2474 |
use 'collect' _md -- Posted via http://www.ruby-forum.com/.
[toc] | [prev] | [next] | [standalone]
| From | Jesús Gabriel y Galán <jgabrielygalan@gmail.com> |
|---|---|
| Date | 2011-04-07 11:50 -0500 |
| Message-ID | <BANLkTikHBUxhYHuLYz41_YyGvLKx+mVjYQ@mail.gmail.com> |
| In reply to | #2474 |
On Thu, Apr 7, 2011 at 6:38 PM, Cyril Joe <cyril_jose@ymail.com> wrote:
> Is there a way to get rid of newlines in an array?
> Example: array = ["hel\nlo", "bl\nah"]
>
> I want the output to be new_array = ["hello", "blah"]
>
> I tried:
>
> array.each do |el|
> el.delete("\n")
> end
>
> But that didn't work. Can't figure this out. Any suggestions?
String#delete returns a copy of the string, it doesn't modify it in
place. Use String#delete!
ruby-1.8.7-p334 :017 > array = ["hel\nlo", "bl\nah"]
=> ["hel\nlo", "bl\nah"]
ruby-1.8.7-p334 :018 > array.each {|word| word.delete!("\n")}
=> ["hello", "blah"]
ruby-1.8.7-p334 :019 > array
=> ["hello", "blah"]
Jesus.
[toc] | [prev] | [next] | [standalone]
| From | Cyril Jose <cyril_jose@ymail.com> |
|---|---|
| Date | 2011-04-07 13:08 -0500 |
| Message-ID | <d8dafb7230749cf4936b85d9d78e86c8@ruby-forum.com> |
| In reply to | #2474 |
Thanks Jesus - exactly what I needed. -- Posted via http://www.ruby-forum.com/.
[toc] | [prev] | [next] | [standalone]
| From | 7stud -- <bbxx789_05ss@yahoo.com> |
|---|---|
| Date | 2011-04-07 13:34 -0500 |
| Message-ID | <d9d8bc4bd3d3c1f79205895b94e3c974@ruby-forum.com> |
| In reply to | #2474 |
each() doesn't create a new array. Why not use map()? And you need to
be careful using delete!() because it will change the strings in the
original array too.
array = ["hel\nlo", "bl\nah"]
new_arr = array.map do |str|
str.delete!("\n")
end
p new_arr
p array
--output:--
["hello", "blah"]
["hello", "blah"]
If you really want to preserve the original array, don't use delete! on
the strings. On the other hand, if you don't need two versions of the
array hanging around in memory, then use all ! methods:
array = ["hel\nlo", "bl\nah"]
array.map! do |str|
str.delete!("\n")
end
p array
--output:--
["hello", "blah"]
--
Posted via http://www.ruby-forum.com/.
[toc] | [prev] | [next] | [standalone]
| From | Jesús Gabriel y Galán <jgabrielygalan@gmail.com> |
|---|---|
| Date | 2011-04-07 14:17 -0500 |
| Message-ID | <BANLkTi=oe-C7RzZjm0+Op7XiUcdwSFNBNg@mail.gmail.com> |
| In reply to | #2481 |
On Thu, Apr 7, 2011 at 8:34 PM, 7stud -- <bbxx789_05ss@yahoo.com> wrote:
> each() doesn't create a new array. Why not use map()? And you need to
> be careful using delete!() because it will change the strings in the
> original array too.
This obviously depends on what he needs. The fact that each doesn't
create a new array can be a good thing :-).
> array = ["hel\nlo", "bl\nah"]
>
> new_arr = array.map do |str|
> str.delete!("\n")
> end
This I don't understand. You are modifying the original strings but
creating a new array with them. What could be the use case for this?
>
> p new_arr
> p array
>
> --output:--
> ["hello", "blah"]
> ["hello", "blah"]
>
>
> If you really want to preserve the original array, don't use delete! on
> the strings. On the other hand, if you don't need two versions of the
> array hanging around in memory, then use all ! methods:
>
> array = ["hel\nlo", "bl\nah"]
>
> array.map! do |str|
> str.delete!("\n")
> end
You don't need map! here, cause you don't want to change which object
each position references. You just want to modify the strings
themselves. What I would say is that, if you need to preserve the
original strings (because they are referenced by other variables) but
use the same array, do:
a = "hel\nlo"
b = "bl\nah"
array = [a,b]
array.map! do |str|
str.delete("\n")
end
The bang version of map, because you want to change the array, but the
non-bang version of delete so as to keep the original strings. The two
cases you propose above have less use cases, IMHO.
Jesus.
[toc] | [prev] | [next] | [standalone]
| From | 7stud -- <bbxx789_05ss@yahoo.com> |
|---|---|
| Date | 2011-04-07 18:29 -0500 |
| Message-ID | <320bd4f1961690b7d4f8335c8e1fed53@ruby-forum.com> |
| In reply to | #2484 |
"Jesús Gabriel y Galán" <jgabrielygalan@gmail.com> wrote in post
#991555:
> On Thu, Apr 7, 2011 at 8:34 PM, 7stud -- <bbxx789_05ss@yahoo.com> wrote:
>> each() doesn't create a new array. Why not use map()? And you need to
>> be careful using delete!() because it will change the strings in the
>> original array too.
>
> This obviously depends on what he needs. The fact that each doesn't
> create a new array can be a good thing :-).
>
While I realize it isn't always definitive, let's re-read what the op
actually said:
>>> Example: array = ["hel\nlo", "bl\nah"]
>>> I want the output to be new_array = ["hello", "blah"]
>> array = ["hel\nlo", "bl\nah"]
>>
>> new_arr = array.map do |str|
>> str.delete!("\n")
>> end
>
> This I don't understand. You are modifying the original strings but
> creating a new array with them. What could be the use case for this?
>
Well, let's see what I said about that:
> 2) You need to be careful using delete!() because
> it will change the strings in the original array too:
"careful" meaning, "Dear op, you don't want to do that".
>> the strings. On the other hand, if you don't need two versions of the
>> array hanging around in memory, then use all ! methods:
>>
>> array = ["hel\nlo", "bl\nah"]
>>
>> array.map! do |str|
>> str.delete!("\n")
>> end
>
> You don't need map! here,
Yeah, I edited that out before you posted. My final suggestion uses
map() and delete().
--
Posted via http://www.ruby-forum.com/.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.ruby
csiph-web