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
On Fri, Jul 9, 2010 at 1:22 PM, Francisco M. [email protected] 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:
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.
On Fri, Jul 9, 2010 at 1:22 PM, Francisco M. [email protected] 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
On Fri, Jul 9, 2010 at 1:22 PM, Francisco M. [email protected] 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 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 2:01 PM, Francisco M. [email protected] 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
line and stick them in the array instead.
Don’t forget too that File objects are fully enumerable, so you don’t
have to maintain an explicit accumulator array – you can use map
instead:
def get_tokens_from_file(filename)
File.open(filename) do |f|
f.map do |line|
get_tokens_from_line(line)
end
end
end
token_array = get_tokens_from_file(filename)
or similar.
David
–
David A. Black, Senior Developer, Cyrus Innovation Inc.
The Ruby training with Black/Brown/McAnally
Compleat Stay tuned for next event announcement!
Rubyist http://www.compleatrubyist.com
On Fri, Jul 9, 2010 at 7:44 PM, Francisco M. [email protected] 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…
On Fri, Jul 9, 2010 at 2:01 PM, Francisco M. [email protected] 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
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…
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.