Solved

delete a record

Posted on 2000-02-28
12
145 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 84

Expert Comment

by:ozo
Comment Utility
print FILE unless /\Q$LTD/;
0
 
LVL 3

Expert Comment

by:guadalupe
Comment Utility
OR:

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

Expert Comment

by:guadalupe
Comment Utility
But then $LTD has to be exactly the whole line...
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

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

Author Comment

by:kudak
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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 …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

772 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

14 Experts available now in Live!

Get 1:1 Help Now