shift changes the array in place:
I looked at an old thread where Bob Hutchinson piped in about
potential GC problems with this, but that was back in '07
[20, 30]
x = arr[1…-1]
new object is created by x =
[10, 20, 30]
Is that really what happens? I thought it just reassigned a starting
point, and indexed from there. At least, that would make more sense
to me if it did.
Would it? What if you had an array that took up 3GB of memory and you
shifted off every element but the last one. Would you expect your array
to still occupy 3GB of memory?
I would hope that the GC could keep up, and I’d plan accordingly if
not. But if your 3GB array was duped/cloned, hmm, is that better?
Please tell me we are talking about two different arguments.
On Mon, Apr 27, 2009 at 12:31 AM, Martin S. [email protected]
wrote:
I looked at an old thread where Bob Hutchinson piped in about
potential GC problems with this, but that was back in '07
[20, 30]
x = arr[1…-1]
new object is created by x =
[10, 20, 30]
Maybe we don’t know exactly what it is you are trying to do. strs
was, in my example, supposed to be a large list separated by new
lines, each containing something like…
number city city city …
I just assumed that you wanted a list with an identifying marker,
which would be most likely a Hash.
You can iterate over hashes, just like arrays, but the nomenclature
can be different.
What do you have as data and what do you want to do with it?
:)I am really confused with ruby now.
OK, what I want to do is this, let me describe by c
input is one line: number city1 city2…city2
for(i=0;i<number;i++)
{
function(cityi)
}
These ruby sentenses
h = {}
strs.each{|i|h[(v=i.split).shift]=v}
can split the city names, but can I loop h{}? how?
Thanks!
Maybe we don’t know exactly what it is you are trying to do. strs
was, in my example, supposed to be a large list separated by new
lines, each containing something like…
number city city city …
I just assumed that you wanted a list with an identifying marker,
which would be most likely a Hash.
You can iterate over hashes, just like arrays, but the nomenclature
can be different.
What do you have as data and what do you want to do with it?
had to use (items = my_item_list.split) inside parens like that
That looks like I’m buoying my idea. I think any single one of these
methods will work just fine. I’m somewhat leaning towards [1…-1]
because it’s very clear what’s happening.
I’ll stick with my #shift for now because it makes logical sense to
me. I’m not overly concerned in my work with speed or memory use.
James knows what he’s talking about so really listen to him.