Solved

delete a record

Posted on 2000-02-28
12
148 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Rename file based on contents in the file? 3 97
git fat pull happens nothing in centos 6.3 python 2.6.6 33 219
Convert grep lines to perl 6 58
Move Function in Perl Script 2 72
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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…
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…

839 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