How to parse a "line"?

M

Martin Sharon

Hi there:

I have a line, the format is like this
"num_of_item item1 item2...item_n" for example

"3 Tokyo Newyork Paris"

I want ruby parse this format, extract these keywords and save them in
an array.
I googled and search many forums, but can't find any matches.

Would anyone give me any ideas??
Thanks a lot!
 
J

James Gray

Hi there:

I have a line, the format is like this
"num_of_item item1 item2...item_n" for example

"3 Tokyo Newyork Paris"

I want ruby parse this format, extract these keywords and save them in
an array.
I googled and search many forums, but can't find any matches.

Would anyone give me any ideas??

If you just need to bust up the words via the spaces, this should
probably work for you:
items = "3 Tokyo Newyork Paris".split[1..-1]
=> ["Tokyo", "Newyork", "Paris"]

James Edward Gray II
 
R

Robert Dober

Hi there:

I have a line, the format is like this
"num_of_item item1 item2...item_n" for example

"3 Tokyo Newyork Paris"

I want ruby parse this format, extract these keywords and save them in
an array.
I googled and search many forums, but can't find any matches.

Would anyone give me any ideas??

If you just need to bust up the words via the spaces, this should probably
work for you:
items = "3 Tokyo Newyork Paris".split[1..-1]
=> ["Tokyo", "Newyork", "Paris"]

This is another opportunity to show Ruby's versatile style, e.g. functional

_, *items = "3 .a b c".split

R.
 
T

Todd Benson

Hi there:

I have a line, the format is like this
"num_of_item item1 item2...item_n" for example

"3 Tokyo Newyork Paris"

I want ruby parse this format, extract these keywords and save them in
an array.
I googled and search many forums, but can't find any matches.

Would anyone give me any ideas??

If you just need to bust up the words via the spaces, this should probably
work for you:
items = "3 Tokyo Newyork Paris".split[1..-1]
=> ["Tokyo", "Newyork", "Paris"]

This is another opportunity to show Ruby's versatile style, e.g. functional

_, *items = "3 .a b c".split

I like...

(items = my_item_list).split.shift

Both you and I have examples, though, that do not return what the
statement using [1..-1] does. Mine, returns 3. Ah well, "items"
exists, though :)

Todd
 
M

Martin Sharon

Thank you Todd, but the number of the keywords are dynamic.

and the array size is not fixed until program reads the first keywords
of the input line.
The line may be "3 tokyo newyork paris", also maybe
"6 toyota bmw honda GM Ford"

So I can't hard coded this array in the program.
How can I parse the line?

Thanks!
 
7

7stud --

Martin said:
Thank you Todd, but the number of the keywords are dynamic.

and the array size is not fixed until program reads the first keywords
of the input line.


Explain why these solutions won't work for you:

strs = ["6 toyota bmw honda GM Ford", "3 tokyo newyork paris"]

strs.each do |str|
puts "1) split with subscripts:"
p str.split()[1..-1]

puts "2) split with parallel assignment:"
first, *therest = str.split()
p therest

puts
end

--output:--
1) split with subscripts:
["toyota", "bmw", "honda", "GM", "Ford"]
2) split with parallel assignment:
["toyota", "bmw", "honda", "GM", "Ford"]

1) split with subscripts:
["tokyo", "newyork", "paris"]
2) split with parallel assignment:
["tokyo", "newyork", "paris"]
 
J

James Gray

Thank you Todd, but the number of the keywords are dynamic.

and the array size is not fixed until program reads the first keywords
of the input line.
The line may be "3 tokyo newyork paris", also maybe
"6 toyota bmw honda GM Ford"

So I can't hard coded this array in the program.
How can I parse the line?

Did you try our code? All of our solutions work for all examples =20
you've posted so far=85 :)

James Edward Gray II=
 
T

Todd Benson

Thank you Todd, but the number of the keywords are dynamic.

and the array size is not fixed until program reads the first keywords
of the input line.
The line may be "3 tokyo newyork paris", also maybe
"6 toyota bmw honda GM Ford"

So I can't hard coded this array in the program.
How can I parse the line?

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
 
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.

You know after looking at your original post, maybe you are trying to
create a digest, i.e. a Hash or associative array, with your keys as
the first "column".

In that case, you might try...

h = {}
f = my_string_list.each_line do |line|
key = (value = line.split).shift
h[key] = value
end

...or something like that.

Todd
 
T

Todd Benson

In that case, you might try...

h = {}
f = my_string_list.each_line do |line|
key = (value = line.split).shift
h[key] = value
end

...or something like that.

It's late and I'm golfing with myself. Apologies to the list for this
almost one-liner...

h = {}
s.each_line{|i|h[(v=i.split).shift]=v}
 
M

Martin Sharon

it works!

s.each instead of s.each_line

thanks!

Todd said:
In that case, you might try...

h = {}
f = my_string_list.each_line do |line|
key = (value = line.split).shift
h[key] = value
end

...or something like that.

It's late and I'm golfing with myself. Apologies to the list for this
almost one-liner...

h = {}
s.each_line{|i|h[(v=i.split).shift]=v}
 
T

Todd Benson

it works!

s.each instead of s.each_line

Using 1.9.1. Sorry I didn't mention that. I haven't been following
the differences, but I seem to recall that was a change somewhere
along the line.

Todd
 
7

7stud --

Todd said:
My way was only slightly different. I opted to create the whole
array, and then drop the first without creating an extra object.

Note that you made ruby go through the laborious chore of shifting every
element in the array over one spot to the left.
 
T

Todd Benson

Note that you made ruby go through the laborious chore of shifting every
element in the array over one spot to the left.

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.

Todd
 
T

Todd Benson

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.

Let me rephrase. What is different on the underlying structure with
[1..-1] and #shift? I'm not sure if shift really moves everything,
but if it does, I wouldn't mind knowing.

Todd
 
H

Harry Kakueki

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.

Let me rephrase. What is different on the underlying structure with
[1..-1] and #shift? I'm not sure if shift really moves everything,
but if it does, I wouldn't mind knowing.

Todd

If it shifts everything, it sure does it fast :)

p Time.now
arr = (1..100_000_000).to_a
p Time.now
arr.shift
p arr[0]
p Time.now


###output

Mon Apr 27 13:34:06 +0900 2009
Mon Apr 27 13:36:29 +0900 2009
2
Mon Apr 27 13:36:29 +0900 2009


Harry
 
7

7stud --

Todd said:
to me if it did.
Let me rephrase. What is different on the underlying structure with
[1..-1] and #shift? I'm not sure if shift really moves everything,
but if it does, I wouldn't mind knowing.

Todd

shift changes the array in place:

1)
arr = [10, 20, 30]

y = arr.shift
puts y
p arr

--output:--
10
[20, 30]


subscripts create a new array:

2)
arr = [10, 20, 30]
p arr
print "id: ", arr.object_id
puts

x = arr[1..-1]
p x
print "id: ", x.object_id
puts
p arr

--output:--
[10, 20, 30]
id: 75950
[20, 30]
id: 75860
[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?
 
T

Todd Benson

Todd said:
to me if it did.
Let me rephrase. What is different on the underlying structure with
[1..-1] and #shift? I'm not sure if shift really moves everything,
but if it does, I wouldn't mind knowing.

Todd

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
1)
arr = [10, 20, 30]

y = arr.shift
puts y
p arr

--output:--
10
[20, 30]


subscripts create a new array:

2)
arr = [10, 20, 30]
p arr
print "id: ", arr.object_id
puts

x = arr[1..-1]

new object is created by x =
p x
print "id: ", x.object_id
puts
p arr

--output:--
[10, 20, 30]
id: 75950
[20, 30]
id: 75860
[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.

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