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!
Todd Benson wrote:
···
On Sun, Apr 26, 2009 at 5:55 PM, Todd Benson <caduceass@gmail.com> > wrote:
Did you try our code? All of our solutions work for all examples you've posted so far…
James Edward Gray II
···
On Apr 26, 2009, at 7:25 PM, Martin Sharon wrote:
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
···
On Sun, Apr 26, 2009 at 7:25 PM, Martin Sharon <huangshuo.9@gmail.com> wrote:
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?
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
···
On Sun, Apr 26, 2009 at 9:11 PM, Todd Benson <caduceass@gmail.com> wrote:
On Sun, Apr 26, 2009 at 7:25 PM, Martin Sharon <huangshuo.9@gmail.com> wrote:
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.
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.
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
···
On Sun, Apr 26, 2009 at 9:11 PM, Todd Benson <caduceass@gmail.com> wrote:
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.
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
···
On Sun, Apr 26, 2009 at 10:59 PM, 7stud -- <bbxx789_05ss@yahoo.com> wrote:
Todd Benson wrote:
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.
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
···
On Sun, Apr 26, 2009 at 11:03 PM, Todd Benson <caduceass@gmail.com> wrote:
On Sun, Apr 26, 2009 at 10:59 PM, 7stud -- <bbxx789_05ss@yahoo.com> wrote:
Todd Benson wrote:
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.
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.
On Mon, Apr 27, 2009 at 1:07 PM, Todd Benson <caduceass@gmail.com> wrote:
On Sun, Apr 26, 2009 at 11:03 PM, Todd Benson <caduceass@gmail.com> wrote:
On Sun, Apr 26, 2009 at 10:59 PM, 7stud -- <bbxx789_05ss@yahoo.com> wrote:
Todd Benson wrote:
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.
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.
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.
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?
···
On Sun, Apr 26, 2009 at 11:03 PM, Todd Benson <caduceass@gmail.com> > wrote: