Solved

delete a record

Posted on 2000-02-28
12
147 Views
Last Modified: 2012-05-04
i want to delete a record in my text file, example:
a|b
d|e
g|h

let's say i want to delete d|e from the web, i have a code below but it seems that it will delete the entire record. anyone can correct this??

open(FILE, "file.txt");
my(@lines) = <FILE>;
close(FILE);
my($i) = 0;
foreach (@lines) {
$FILELINES{$_} = $i + 1;
$i++;
}
my($LTD) = $FORM{'name'} . "|" . $FORM{'address'} . "\n";
$LineToDelete = $FILELINES{$LTD};
$Numbs = @lines;
open(FILE, ">file.txt");
     for($i = 0; $i < $Numbs - 1; $i++) {
          print FILE @lines[$i] unless ($i = ($LineToDelete - 1));
     }
close(FILE);

0
Comment
Question by:kudak
12 Comments
 
LVL 1

Expert Comment

by:olthoff
ID: 2565039
open(FILE, 'file.txt');
my (@lines) = <FILE>;
close(FILE);
$i = 1;

foreach (@lines) {
  $FILELINES{$_} = $i++;
};
my $LTD = $FORM{name} . '|' . $FORM{address} . "\n";
delete($FILELINES{$LTD});
open(FILE '>file.txt');
foreach (keys %FILELINES) {
  print(FILE $_);
};
close(FILE);
0
 
LVL 3

Accepted Solution

by:
guadalupe earned 10 total points
ID: 2565060
Try this:

open(FILE, "file.txt");
my(@lines) = <FILE>;
close(FILE);
my($i) = 0;

my($LTD) = $FORM{'name'} . "|" . $FORM{'address'} . "\n";

open(FILE, ">file.txt");

foreach (@lines)
{
      print FILE unless /$LTD/;
}


close(FILE);
0
 

Author Comment

by:kudak
ID: 2571236
dear quadalupe,
i've tried yours but there's a small problem.

let's say i have a file contains:

a|b|c
d|e|f
g|h|i
k|l|a

if i try to run your program by key in
a,b,c ffrom the browser, i will delete the first and the last line.
it's supposed to delete only the first line. i do not know why it deletes the last one as well.
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 84

Expert Comment

by:ozo
ID: 2571393
print FILE unless /\Q$LTD/;
0
 
LVL 3

Expert Comment

by:guadalupe
ID: 2572341
OR:

print FILE unless /^$LTD$/;  
0
 
LVL 3

Expert Comment

by:guadalupe
ID: 2572347
But then $LTD has to be exactly the whole line...
0
 

Author Comment

by:kudak
ID: 2572426
i've tried what ozo suggested, but it could not even delete the record that i've keyed in.
0
 

Author Comment

by:kudak
ID: 2572443
i've also tried yours guadalupe,
i insert your new code,
 print FILE unless /^$LTD$/;  
does not delete at all.
the file remains the same.
0
 
LVL 3

Expert Comment

by:guadalupe
ID: 2572604
Are you certain that $LTD contains the exact line in its entirety?  What about adding a new lione just to see if that is waht is being bothersome... like this before the matching:

$LTD .= "\n";

Also are you paying attention to caps and lower could you be mixing case between the value of $LTD and the actual line.  If you want to avoid such a problem edit the line again like this:

print FILE unless /^$LTD$/i;  


0
 

Author Comment

by:kudak
ID: 2575233
well guadalupe,
it's still has some errors. now the code is like you ask me to do. it will delete whenever the find the same record.
let's say when user key in a for the first input field, b for the second and so forth, and let's say i have this record in my xxx1.txt file,
a|b|c|d|e|f|g
1|2|3|4|5|6|7
waed|fs|ds|f|vf|ds|a

the first line and the last is deleted, perhaps because of the same record, a and f

open(FILE, "xxx1.txt");
my(@lines) = <FILE>;
close(FILE);
my($i) = 0;

$LTD .= "\n";
my($LTD) = $FORM{'masa'} . "|" . $FORM{'hari'} . "|" . $FORM{'bulan'} . "|" . $FORM{'sumber'} . "|" . $FORM{'nama'} . "|" . $FORM{'email'} . "|" . $FORM{'tujuan'} . "\n";

open(FILE, ">xxx1.txt");

foreach (@lines)
{
print FILE unless /^$LTD$/i;  
#print FILE unless /\Q$LTD/;
}


close(FILE);
0
 
LVL 3

Expert Comment

by:guadalupe
ID: 2576368
Wait there is an error...

If you are explicitly adding the new line in this line:

my($LTD) = $FORM{'masa'} . "|" . $FORM{'hari'} . "|" . $FORM{'bulan'} . "|" . $FORM{'sumber'} . "|" . $FORM{'nama'} . "|" . $FORM{'email'} . "|" . $FORM{'tujuan'} . "\n";

Then you don't need my suggestion of $LTD .= "\n" ;

which by the way was meant to come after the the assigment of $LTD...




0
 
LVL 3

Expert Comment

by:guadalupe
ID: 2576395
OPK I'm sorry Ididn't notice it before... Its you pipe.  You putting a sting of chars into $LTD serperated by a pipe but then you using that string as a reguklar expression in a pattern match.  In regex the pipe means or.  We're throwing the whole thing out of wack with all those ors.

Solution:

Change the field seperator to ":"  both in the xxx.txt file and in the scrpt line:

my($LTD) = $FORM{'masa'} . "|" . $FORM{'hari'} . "|" . $FORM{'bulan'} . "|" . $FORM{'sumber'} . "|" . $FORM{'nama'} . "|" . $FORM{'email'} . "|" . $FORM{'tujuan'} . "\n";

That should do it..

0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Trying to use two loops in a perl script but not getting results need. 10 88
Convert grep lines to perl 6 35
Replace  text in a file 2 104
Perl tar error 8 64
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now