CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
How to remove a line?

 



Anthony
Deleted

Mar 22, 2000, 7:49 PM

Post #1 of 3 (871 views)
How to remove a line? Can't Post

I have a text file with multiple entries like the following:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


$username|$date
$username|$date
... etc
</pre><HR></BLOCKQUOTE>

I have a subroutine that looks like this so far:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


open (WHOWRITE,">$filename") &#0124; &#0124; die "Egads!: $!";
flock(WHOWRITE, 2);
foreach $lines(@user_array) {
@edit_array = split(/\|/, $lines);
$late = $date + 600;
if ($edit_array[1] >= $late {
## remove entire $username|$date line from the file
}
</pre><HR></BLOCKQUOTE>

See what I am going for? I cannot for the life of me (esp. @ 11:47pm) resolve how to do as my comment states.

Any direction or assistance would be appreciated.

Thanks

Anthony


japhy
Enthusiast

Mar 23, 2000, 5:49 AM

Post #2 of 3 (871 views)
Re: How to remove a line? [In reply to] Can't Post

Use the joy of Perl's built-in in-place file editing. What I'm about to show is documented in 'perlvar' and 'perlrun', among others. Also look in 'perlfaq5' for the question about changing one line in a file (or deleting, or inserting, etc.).

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


{
local $^I = ""; # change to ".bak" if you want a backup extension
local @ARGV = $filename;
while (<> ) {
chomp(($user,$date) = split /\|/);
unless ($date >= $late) { print }
}
}
</pre><HR></BLOCKQUOTE>

Modify that to do what you specifically need -- I was a bit puzzled by how you were determining how to remove a line.


Anthony
Deleted

Mar 24, 2000, 6:36 PM

Post #3 of 3 (871 views)
Re: How to remove a line? [In reply to] Can't Post

Okay, I've written something (at 4 in the morning) and plugged it into my main script, but it just throws a compilation error.

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


#################
# Sub WhoCheck #
#################
sub WhoCheck {
$date = time()

open (WHOREAD,"<$reg_dir/$room_who.txt") &#0124; &#0124; die "Egads!: $!";
flock(WHOREAD, 2);
@user_array = <WHOREAD>;
flock(WHOREAD, 8);
close (WHOREAD);

open (WHOWRITE,">$reg_dir/$room_who.txt") &#0124; &#0124; die "Egads!: $!";
flock(WHOWRITE, 2);
foreach $lines(@user_array) {
@edit_array = split(/\|/, $lines);
$late = $date + 600;
if ($edit_array[1] >= $late {
local $^I = ""; ################ edit in place ################
local @ARGV = $reg_dir/$room_who.txt;
while (<> ) {
chomp(($user,$date) = split /\|/);
}
} elsif ($edit_array[0] eq $user) {
$edit_array[1] = $date;
} else { &append;
}
}
flock(WHOWRITE, 8);
close (WHOWRITE);
}
#####################
# End Sub WhoCheck #
#####################

###############
# Sub append #
###############
sub append {

open (APPEND,">>$reg_dir/$room_who.txt") &#0124; &#0124; die "Egads!: $!";
flock(APPEND, 2);
print APPEND ("$user|$date\n");
flock(APPEND, 8);
close (APPEND);
}
###################
# End Sub append #
###################
</pre><HR></BLOCKQUOTE>

Eh, is that just sloppy coding or what? Any ideas as to what is throwing the error?

Anthony

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives