I'm doing one work school, sorry don't write english very good, I'm
brasilian. I want delete only one line of File.
I have two files and have to handle them according
with data received by users in case some have
Students enrolled in the following way name! id: registration.
The user enters the id of the student, I have to search
the file and delete the row for that id. Since there
I found nothing specific files I'm using
delete_if, look at the code, I'll attach it.
input.each do |n|
if n == 4
# skip it
else
output << n
end
end
p output #=> [1, 2, 3, 5, 6, 7, 8, 9, 10]
Working with files, I might do something like:
* Create a temporary output file
* Write to the output file while reading from the original input
* When finished, move temporary file over the original
···
On Wed, Mar 9, 2011 at 9:55 PM, Joyce Lima <jojosl@hotmail.com> wrote:
I'm doing one work school, sorry don't write english very good, I'm
brasilian. I want delete only one line of File.
I have two files and have to handle them according
with data received by users in case some have
Students enrolled in the following way name! id: registration.
The user enters the id of the student, I have to search
the file and delete the row for that id. Since there
I found nothing specific files I'm using
delete_if, look at the code, I'll attach it.
# First you should read the file:
alunos = IO.readlines('alunos.txt') # return an array with all ids
# Remove the selected id:
alunos.delete_if do |linha|
dados = linha.split(":") #"quebra" quando encontrar dois pontos
outrosDados = dados[0].split("!")
outrosDados == login
end
# Open the file and rewrite the content
f = File.open('alunos.txt', 'w')
alunos.each |a|
f.puts a
end
f.close
This should work!
···
On Wed, Mar 9, 2011 at 11:55 PM, Joyce Lima <jojosl@hotmail.com> wrote:
I'm doing one work school, sorry don't write english very good, I'm
brasilian. I want delete only one line of File.
I have two files and have to handle them according
with data received by users in case some have
Students enrolled in the following way name! id: registration.
The user enters the id of the student, I have to search
the file and delete the row for that id. Since there
I found nothing specific files I'm using
delete_if, look at the code, I'll attach it.
# First you should read the file:
alunos = IO.readlines('alunos.txt') # return an array with all ids
# Remove the selected id:
alunos.delete_if do |linha|
dados = linha.split(":") #"quebra" quando encontrar dois pontos
outrosDados = dados[0].split("!")
outrosDados == login
end
# Open the file and rewrite the content
This should work!
...unless your computer crashes right after you open the file. You
have to use a temporary file as unkown posted. The Tempfile module is
helpful in that regard.
# First you should read the file:
alunos = IO.readlines('alunos.txt') # return an array with all ids
# Remove the selected id:
alunos.delete_if do |linha|
dados = linha.split(":") #"quebra" quando encontrar dois pontos
outrosDados = dados[0].split("!")
outrosDados == login
end
# Open the file and rewrite the content
This should work!
...unless your computer crashes right after you open the file. To
guard against losing all your data, you have to use a temporary file as
unkown posted. The Tempfile module is helpful in that regard.
# First you should read the file:
alunos = IO.readlines('alunos.txt') # return an array with all ids
# Remove the selected id:
alunos.delete_if do |linha|
dados = linha.split(":") #"quebra" quando encontrar dois pontos
outrosDados = dados[0].split("!")
outrosDados == login
end
# Open the file and rewrite the content
This should work!
...unless your computer crashes right after you open the file. To
guard against losing all your data, you have to use a temporary file
as--unkown posted. The Tempfile module is helpful in that regard.