Hi...I'm trying to solve a very simple exercise but this is one of my
first tasks and I can't manage to find a solution.
I have a txt file and I have to write a method which; given the txt file
as an entry argument returns an array of tokens. I have to call the
method that prints the solutin in the screen.
The solution should be this type:
token 1= The
token 2= man
token 3= was
The starting txt file is a normal text…the only thing is that each
word or diacritic simbol is separated from a space to the other one. I
suppose I have to solve the problem with a regular expression but for
example I don't know how to punt words in the array and how to put them
one per line as the solution shows.
Some suggestion???
Thanks
You can put individual elements into the array w/ the push operator
"<<":
myArray << "someTxt"
Then, if you want to print each value in the array, you would use
the .each iterator:
myArray.each do |item|
puts item
end
Between these two, you should be able to easily add the supporting code
you'll need to get your results.
-Alex
···
On Fri, 2010-07-09 at 12:22 -0500, Francisco Martinez wrote:
Hi...I'm trying to solve a very simple exercise but this is one of my
first tasks and I can't manage to find a solution.
I have a txt file and I have to write a method which; given the txt file
as an entry argument returns an array of tokens. I have to call the
method that prints the solutin in the screen.
The solution should be this type:
token 1= The
token 2= man
token 3= was
The starting txt file is a normal text...the only thing is that each
word or diacritic simbol is separated from a space to the other one. I
suppose I have to solve the problem with a regular expression but for
example I don't know how to punt words in the array and how to put them
one per line as the solution shows.
Some suggestion???
Thanks
How far have you made it?
* read from the file
* get the tokens from the file
* put the tokens into an array
* display the output
···
On Fri, Jul 9, 2010 at 1:22 PM, Francisco Martinez <calabazag@hotmail.es> wrote:
Hi...I'm trying to solve a very simple exercise but this is one of my
first tasks and I can't manage to find a solution.
I have a txt file and I have to write a method which; given the txt file
as an entry argument returns an array of tokens. I have to call the
method that prints the solutin in the screen.
The solution should be this type:
Hi...I'm trying to solve a very simple exercise but this is one of my
first tasks and I can't manage to find a solution.
I have a txt file and I have to write a method which; given the txt file
as an entry argument returns an array of tokens. I have to call the
method that prints �the solutin in the screen.
The solution should be this type:
token 1= The
token 2= man
token 3= was
How far have you made it?
* read from the file
* get the tokens from the file
* put the tokens into an array
* display the output
I think I firstly have to do sometingh like this:
def print_tokens(txtFile)
f=File.open(txtFile)
f.each do |line|
if(line=~/\t(.+)/)
puts("token= #{$1}")
end
f.close
end
end
Here I don't have any array and something to indicate the number of each
line...and it doesnt't work
···
On Fri, Jul 9, 2010 at 1:22 PM, Francisco Martinez > <calabazag@hotmail.es> wrote:
You can put individual elements into the array w/ the push operator
"<<":
myArray << "someTxt"
Then, if you want to print each value in the array, you would use
the .each iterator:
myArray.each do |item|
puts item
end
Between these two, you should be able to easily add the supporting code
you'll need to get your results.
-Alex
I Have done It:
num=1
File.open("text.txt")
puts "token#{num}= #{nom}"
num=num+1
end
it reads the wole text not considering each word or "." but reading each
line...I need to put it in a method that calls "text.txt" and to use an
array and the result must be each line one word.
Hi...I'm trying to solve a very simple exercise but this is one of my
first tasks and I can't manage to find a solution.
I have a txt file and I have to write a method which; given the txt file
as an entry argument returns an array of tokens. I have to call the
method that prints �the solutin in the screen.
The solution should be this type:
token 1= The
token 2= man
token 3= was
How far have you made it?
* read from the file
* get the tokens from the file
* put the tokens into an array
* display the output
I Have done It:
num=1
File.open("text.txt")
puts "token#{num}= #{nom}"
num=num+1
end
it reads the wole text not considering each word or "." but reading each
line...I need to put it in a method that calls "text.txt" and to use an
array and the result must be each line one word.
···
On Fri, Jul 9, 2010 at 1:22 PM, Francisco Martinez > <calabazag@hotmail.es> wrote:
On Jul 9, 12:44 pm, Francisco Martinez <calaba...@hotmail.es> wrote:
unknown wrote:
> On Fri, Jul 9, 2010 at 1:22 PM, Francisco Martinez > > <calaba...@hotmail.es> wrote:
>> Hi...I'm trying to solve a very simple exercise but this is one of my
>> first tasks and I can't manage to find a solution.
>> I have a txt file and I have to write a method which; given the txt file
>> as an entry argument returns an array of tokens. I have to call the
>> method that prints the solutin in the screen.
>> The solution should be this type:
>> token 1= The
>> token 2= man
>> token 3= was
> How far have you made it?
> * read from the file
> * get the tokens from the file
> * put the tokens into an array
> * display the output
I think I firstly have to do sometingh like this:
def print_tokens(txtFile)
f=File.open(txtFile)
f.each do |line|
if(line=~/\t(.+)/)
puts("token= #{$1}")
end
f.close
end
end
Here I don't have any array and something to indicate the number of each
line...and it doesnt't work
it reads the wole text not considering each word or "." but reading each
line...I need to put it in a method that calls "text.txt" and to use an
array and the result must be each line one word.
Assuming you had a method that printed each line of the file like:
f=File.open(txtFile)
f.each do |line|
puts line
end
f.close
You can put individual elements into the array w/ the push operator
"<<": myArray << "someTxt"
(One way that) You could modify it to put each line in an array like:
a =
f=File.open(txtFile)
f.each do |line|
a << line
end
f.close
Of course, in your case you would want to extract the tokens from the
line and stick them in the array instead.
···
On Fri, Jul 9, 2010 at 2:01 PM, Francisco Martinez <calabazag@hotmail.es> wrote:
On Fri, Jul 9, 2010 at 1:31 PM, Alex Stahl <astahl@hi5.com> wrote:
On Fri, Jul 9, 2010 at 2:01 PM, Francisco Martinez > <calabazag@hotmail.es> wrote:
it reads the wole text not considering each word or "." but reading each
line...I need to put it in a method that calls "text.txt" and to use an
array and the result must be each line one word.
Assuming you had a method that printed each line of the file like:
f=File.open(txtFile)
f.each do |line|
puts line
end
f.close
On Fri, Jul 9, 2010 at 1:31 PM, Alex Stahl <astahl@hi5.com> wrote:
You can put individual elements into the array w/ the push operator
"<<": myArray << "someTxt"
(One way that) You could modify it to put each line in an array like:
a =
f=File.open(txtFile)
f.each do |line|
a << line
end
f.close
Of course, in your case you would want to extract the tokens from the
line and stick them in the array instead.
Tank you very much...muy problem is that I don't know how to extract
each token...because in this way I'm putting lines and I don't know how
to print the tokens...I think I should use a regular expression...
--
Posted via http://www.ruby-forum.com/\.
On Fri, Jul 9, 2010 at 7:44 PM, Francisco Martinez <calabazag@hotmail.es> wrote:
Tank you very much...muy problem is that I don't know how to extract
each token...because in this way I'm putting lines and I don't know how
to print the tokens...I think I should use a regular expression...