delete one line of file

J

Joyce Lima

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.

Attachments:
http://www.ruby-forum.com/attachment/6016/bd.rb
 
B

brabuhr

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.

(If I understand, we are looking at opcao == 2 (Excluir Aluno)?)

Here is a small example of one way to copy from one array to another:

input = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
output = []

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
 
R

Raja

[Note: parts of this message were removed to make it a legal post.]

is there any off campus for mca 2011 pass out in india means , plz inform
thank u

regards
j.raja
 
T

Thiago Lewin

[Note: parts of this message were removed to make it a legal post.]

Hi Joyce,

# 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!
 
M

Marc Heiler

I myself use File.readlines but it seems IO.readlines is doing the same
- reading into an array.

When you have an array with the data you can just omit part of that
array and then store that result.

Perhaps there is a more elegant approach to it but it is what I usually
do when I store a modified file.
 
7

7stud --

Thiago Lewin wrote in post #986702:
Hi Joyce,

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

7stud --

Thiago Lewin wrote in post #986702:
Hi Joyce,

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

7stud --

Thiago Lewin wrote in post #986702:
Hi Joyce,

# 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.
 
J

Joyce Lima

Thank you all, I understood the idea of a temporary file and I think
this will solve my problem.
 

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,995
Messages
2,570,228
Members
46,817
Latest member
AdalbertoT

Latest Threads

Top