Solved

delete a record

Posted on 2000-02-28
12
150 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Six Sigma Control Plans

688 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