How to parse a "line"?

T

Todd Benson

h = {}

strs.each{|i|h[(v=i.split).shift]=v}

The array size is 1 by this sentense...

Todd said:
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]

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?

Todd
 
M

Martin Sharon

:)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!
 
M

Mark Thomas

:)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)

}

Is that all you want to do? Once you have the cities,

cities.each do |city|
function(city)
end

So, putting it all together,

lines.each do |line|
(cities = line.split).shift
cities.each do |city|
function(city)
end
end

You don't really need the number at all.

-- Mark.
 
T

Todd Benson

If you look closely, you'll see that James' method grabs everything
from 1 to -1 (the end) of the array, omitting the zeroth element.

Robert's method assigns the garbage -- in this case, the whole array
-- to a dummy variable, and the important stuff to the var that you
care about (items).

My way was only slightly different. I opted to create the whole
array, and then drop the first without creating an extra object. I
had to use (items = my_item_list.split) inside parens like that
because #shift returns the object you popped off the front of the
list, and not the actual remaining stuff.

In all examples given, the size doesn't really matter... hah! Or as
they say, it depends :)

There are probably several ways to do this; I just like that particular one.

hth,
Todd

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.

Todd
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,489
Latest member
BrigidaD91

Latest Threads

Top