FIRST PROGRAMMING PROBLEM Array

  • Thread starter Francisco Martinez
  • Start date
F

Francisco Martinez

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
 
A

Alex Stahl

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
 
B

brabuhr

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 =A0the solutin in the screen.
The solution should be this type:

token 1=3D The
token 2=3D man
token 3=3D 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
 
F

Francisco Martinez

unknown said:
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
 
F

Francisco Martinez

Alex said:
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.
 
F

Francisco Martinez

unknown said:
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.
 
W

w_a_x_man

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


IO.read( "notes.txt" ).split.each_with_index{|s,i|
puts "token #{i} = #{s}"
}
 
B

brabuhr

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

Francisco Martinez

unknown said:
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...
 
D

David A. Black

Hi --

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

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
 

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

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top