Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
SlickEdit: Code in over 40 languages across 7 platforms. SlickEdit’s unmatched power, speed, and flexibility allows even the most accomplished developers to write better code faster. Download a free trial today!
  #1  
Old February 13th, 2001, 12:18 PM
Keshav Keshav is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2000
Location: Chennai, Tamilnadu, India
Posts: 19 Keshav User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hi
I am using a flat file (e.g info.txt) to retrieve data. i want to get the input from a text field of form (for e.g say name) .i want to delete and modify a particular record by using the name from form input. i have used tab for seperating the fields of the record in the text file. Can anyone help me how to do that?

Thanks in Advance
Keshav

Reply With Quote
  #2  
Old February 13th, 2001, 01:00 PM
dsb dsb is offline
PerlGuy
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2001
Posts: 714 dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Days 15 h 44 m 20 sec
Reputation Power: 36
Send a message via AIM to dsb
Wink

You could use some conditional statements and push the records you want to keep onto an array. Then print that array to a new file. If you do it correctly in the logic you shouldn't have to worry about deleting the old file either.
__________________
- dsb -
Perl Guy

Reply With Quote
  #3  
Old February 13th, 2001, 01:19 PM
Keshav Keshav is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2000
Location: Chennai, Tamilnadu, India
Posts: 19 Keshav User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hi Dsb
Can You give me some example source code for that?

Thanks
Keshav

Reply With Quote
  #4  
Old February 13th, 2001, 01:34 PM
dsb dsb is offline
PerlGuy
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2001
Posts: 714 dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Days 15 h 44 m 20 sec
Reputation Power: 36
Send a message via AIM to dsb
Wink

Are you trying to learn Perl? Or are you just trying to use it to get this done?

If you are trying to learn Perl then I'd suggest you try it yourself first. I'd be glad to help you along the way( feel free to email me ), but I think you'd learn better if you took a crack at it before I give you the code.

Not to be a pain in the ***, but you'll understand it better if you find your own way there.

I will help you though.

Reply With Quote
  #5  
Old February 13th, 2001, 01:55 PM
Keshav Keshav is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2000
Location: Chennai, Tamilnadu, India
Posts: 19 Keshav User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
hi

Hi dsb,
Thanks for your immediate response. I am learning and also it should be get done. I had tried to work it. But its not working. I have given my code below. Please check it out and make it work.

Thanks
Keshav

#!/usr/bin/perl
use CGI;
print"content-type:text/html\n\n";
my $action = new CGI;
my $username = $action->param('username');

my $file = "members.txt";
my $old = $file;
my $new = "$file.tmp.$$";
my $bak = "$file.bak";
$found = undef;
open(OLD, "< $old") or die "can't open $old: $!";
open(NEW, "> $new") or die "can't open $new: $!";
@data = <OLD>;
close (OLD);
LOOP: foreach $row(@data){
#my $row = $_;
chomp($row);
#my @currentrow = ();
@currentrow = split(/\t/, $row);
#$currentrow = @currentrow;
if ($username eq $currentrow[0]){
$found = 1;
last LOOP;
}
}
if (defined($found))
{
push @currentrow;
exit;
}
close(OLD) or die "can't close $old: $!";
close(NEW) or die "can't close $new: $!";

rename($old, $bak) or die "can't rename $old to $bak: $!";
rename($new, $old) or die "can't rename $new to $old: $!";
else
{
print "invalid name";
exit;
}

Reply With Quote
  #6  
Old February 13th, 2001, 02:07 PM
dsb dsb is offline
PerlGuy
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2001
Posts: 714 dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Days 15 h 44 m 20 sec
Reputation Power: 36
Send a message via AIM to dsb
Wink

All right, for the sake of time, I'll give you an example of what I need and then you can incorporate where you need it. I'll comment it to explain what I'm doing.

Code:
open( OLD, "filename.txt" ) || die "no!\n";  # you don't need the arrows to signify readability it will default to read in

while ( <OLD> ) {
    chomp;    # get read of the newline at the end of the line
    # here you would put the code
    # that determines whether or not 
    # you want to keep that line
    if ( <condition to determine keeping> ) {
        # to keep
        push( @lines, $_ );
    } else {
        next;  # will go on to next line - no need to do anything else
    }
}
close(OLD);

open( NEW, ">filename.txt" ) || die "no!\n";  # using same filename will overwrite old file so you won't need to delete it later
foreach $n ( 0 .. $#lines ) {   # loop to print array of lines to new file
    print NEW $lines[$n], "\n";
}
close(NEW);


I didn't try this code so you may need to tweak it a little bit. I'll help anywhere I can, but make sure you understand what's going on here.

Hope this Helps!

Reply With Quote
  #7  
Old February 13th, 2001, 04:02 PM
Keshav Keshav is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2000
Location: Chennai, Tamilnadu, India
Posts: 19 Keshav User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
hi

Hi dsb

I tried with ur code. but it is deleting all the records in the file. I want to delete a particular record.

Keshav

Reply With Quote
  #8  
Old February 13th, 2001, 04:07 PM
dsb dsb is offline
PerlGuy
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2001
Posts: 714 dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Days 15 h 44 m 20 sec
Reputation Power: 36
Send a message via AIM to dsb
Wink

I didn't do any record manipulation in my code. If things are getting deleted the problem is in your code.

I took a look at your code and I saw that when you try to push onto '@currentrow' you don't specify an argument to push onto the array. Look into that.

Hope that helps.

Reply With Quote
  #9  
Old February 13th, 2001, 04:10 PM
Keshav Keshav is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2000
Location: Chennai, Tamilnadu, India
Posts: 19 Keshav User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hi,
You don't see the old code. Here is the new one. Check it and reply me.
thanks
Keshav

#!/usr/bin/perl
use CGI;
print"content-type:text/html\n\n";
my $action = new CGI;
my $username = $action->param('username');
my $file = "members.txt";
my $old = $file;
open(OLD, "< $old") or die "can't open $old: $!";
while (<OLD>) {
my $nowRow = $_;
chomp($nowRow);
my @currentRow = ();
@currentRow = split(/\t/, $nowRow);
if ($username eq $currentrow[0]){
push( @currentRow, $nowRow );
} else {
next; # will go on to next line
}
}
close(OLD);
open( NEW, ">members.txt" ) || die "no!\n";
foreach $n ( 0 .. $#lines ) { # loop to print array of lines to new file
print NEW $lines[$n], "\n";
}
close(NEW);

Reply With Quote
  #10  
Old February 13th, 2001, 04:17 PM
dsb dsb is offline
PerlGuy
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2001
Posts: 714 dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Days 15 h 44 m 20 sec
Reputation Power: 36
Send a message via AIM to dsb
Wink


In this section, where you write to a new file:
Code:
open( NEW, ">members.txt" ) || die "no!\n"; 
foreach $n ( 0 .. $#lines ) { # loop to print array of lines to new file 
print NEW $lines[$n], "\n"; 
} 
close(NEW); 


Replace every instance of 'lines' with the name of your array which i believe is currentRow. So it will look like this:
Code:
open( NEW, ">members.txt" ) || die "no!\n"; 
foreach $n ( 0 .. $#currentRow ) { # loop to print array of lines to new file 
print NEW $currentRow[$n], "\n"; 
} 
close(NEW); 

Reply With Quote
  #11  
Old February 13th, 2001, 04:46 PM
Keshav Keshav is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2000
Location: Chennai, Tamilnadu, India
Posts: 19 Keshav User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hi
I tried like that also. But it is deleting the whole file.

Keshav

Reply With Quote
  #12  
Old February 13th, 2001, 04:53 PM
dsb dsb is offline
PerlGuy
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2001
Posts: 714 dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Days 15 h 44 m 20 sec
Reputation Power: 36
Send a message via AIM to dsb
Wink

are you sure you are getting a return value when you assign a value to '$username'...?

Reply With Quote
  #13  
Old February 13th, 2001, 04:57 PM
Keshav Keshav is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2000
Location: Chennai, Tamilnadu, India
Posts: 19 Keshav User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hi
Yes $username is correct. I am able to get that parameter and able to print it. I checked it by print $username;.
But still its not working i don't know why. pls help me.

Keshav

Reply With Quote
  #14  
Old February 13th, 2001, 05:00 PM
dsb dsb is offline
PerlGuy
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2001
Posts: 714 dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Days 15 h 44 m 20 sec
Reputation Power: 36
Send a message via AIM to dsb
Wink

check by the line:
while ( <OLD> ) {

is there a colon there that shouldn't be there?

Reply With Quote
  #15  
Old February 13th, 2001, 05:05 PM
Keshav Keshav is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2000
Location: Chennai, Tamilnadu, India
Posts: 19 Keshav User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
No Colon is not there. I have given the source below. Pls check it line by line and reply me.

Keshav



#!/usr/bin/perl
use CGI;
print"content-type:text/html\n\n";
my $action = new CGI;
my $username = $action->param('username');
open(OLD, "members.txt") or die "can't open $old: $!";
while (<OLD>) {
my $nowRow = $_;
chomp($nowRow);
my @currentRow = ();
@currentRow = split(/\t/, $nowRow);
if ($username eq $currentrow[0]){
push( @currentRow, $nowRow );
} else {
next; # will go on to next line
}
}
close(OLD);
open( NEW, ">members.txt" ) || die "no!\n";
foreach $n ( 0 .. $#currentRow ) {
# loop to print array of lines to new file
print NEW $currentRow[$n], "\n";
}
close(NEW);

Reply With Quote
Reply