LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-31-2003, 04:35 PM   #1
ocularbob
Member
 
Registered: Nov 2002
Location: brooklyn NYC
Distribution: gentoo
Posts: 212

Rep: Reputation: 30
PERL:: trying to remove last line of file


i am trying to remove the last line of a text file like this:

open(DATFILE,">>$DATFILE") || die "no dice";
flock(DATFILE, $LOCK_EXCLUSIVE);
@DATA = <DATFILE>;
$N = 0;
foreach $LINE (@DATA) {
$N = ++$N;
}
truncate (DATFILE, "--$N");
close(DATFILE);
}

but it wipes the whole file.
where did i go wrong?

thanks
 
Old 08-31-2003, 11:08 PM   #2
SaTaN
Member
 
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223

Rep: Reputation: 33
Re: PERL:: trying to remove last line of file

Quote:
Originally posted by ocularbob
i am trying to remove the last line of a text file like this:

open(DATFILE,">>$DATFILE") || die "no dice";

The error is opening the file in the append mode .
In perl when you open the file in append mode , the file pointer goes to the end of the file .

Try this code

open(DATFILE,">>my_file.txt") || die "no such file";
flock(DATFILE, $LOCK_EXCLUSIVE);
@DATA = <DATFILE>;
print @DATA;

The output will be nothing because we are already at the end
of the file.

Plz do tell me if I am wrong.

Last edited by SaTaN; 08-31-2003 at 11:10 PM.
 
Old 08-31-2003, 11:57 PM   #3
ocularbob
Member
 
Registered: Nov 2002
Location: brooklyn NYC
Distribution: gentoo
Posts: 212

Original Poster
Rep: Reputation: 30
well that sounds logical to me.
Does My count loop look right?
While messing around with the code I was able to delete everything in the file with truncate() after opening with ">>$DATFILE".
I also added the $N used as a counter in the code I posted just to see if that was counting up but it never seemed to.
it may be that i collected the value for $N at the wrong time.
gonna try ">$DATFILE" and see what happens.
thanks
 
Old 09-01-2003, 12:05 AM   #4
SaTaN
Member
 
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223

Rep: Reputation: 33
If you use '>$DATFILE' i.e , you are opening the file in write mode
Then , the entire file would become empty after the line

open(DATFILE,">my_file.txt") || die "no such file";

So , I suppose the correct way would be to


open(DATFILE,"my_text.txt") || die "no read";
flock(DATFILE, $LOCK_EXCLUSIVE);
@DATA = <DATFILE>;
close(DATFILE);
open(DATFILE,">my_text.txt") || die "no write";
for($i=0;$i<$#DATA;$i++)
{
print DATFILE $DATA[$i];
}
close(DATFILE);

Last edited by SaTaN; 09-01-2003 at 12:08 AM.
 
Old 09-01-2003, 09:11 AM   #5
coolman0stress
Member
 
Registered: Jun 2003
Location: Toronto, Ontario, Canada
Posts: 288

Rep: Reputation: 30
As far as i know (from my own use) truncate receives a position in bytes from which to truncate the file from. So if you pass it "1" it will get rid of everything past the first byte. So you'll need the seek() and tell() functions to do what you want.

A quick test that i wrote:
Code:
open FILE, "+<data.txt"; # reading and other options
my @all= <FILE>; # just to know the total number of lines through $#all(index of last element)
seek(FILE, 0, 0); # go back to beginning of file
my $count=0; 
while(<FILE>) # go line by line through the file
{
    if($count==$#all-1) # if you are at the second last line
    {
          truncate(FILE, tell(FILE)); # get the current position in file (we are at the start of the last line) and truncate all from that position
    }
    $count++; # increment
}
I'm sure there are better ways to achieve the same thing, but it works, so i thought i'd post it anyway.

Hope this helps

Last edited by coolman0stress; 09-01-2003 at 09:14 AM.
 
Old 09-01-2003, 10:14 AM   #6
ocularbob
Member
 
Registered: Nov 2002
Location: brooklyn NYC
Distribution: gentoo
Posts: 212

Original Poster
Rep: Reputation: 30
well I tried Satan's last snippet. and it erased everything in the file.
coolman's suggestion did not do anything.
I'm at a bit of a loss. what is "$#N" versus "$N"
I think i like opening the file, reading all lines to @DATA and then overwriting the file and printing all the lines back but the last one.
Thats what satan was going for i think.
I'm gonna crack this nut somehow.
thanks for your help so far.
 
Old 09-01-2003, 10:22 AM   #7
SaTaN
Member
 
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223

Rep: Reputation: 33
Quote:
Originally posted by ocularbob
well I tried Satan's last snippet. and it erased everything in the file.

No , my code works fine . I have checked it on my P.C and it worked fine removing the last line ......

Even coolman's code works grt.

Dunno why it doesn't work on your P.C .

Plz check again ....



Reply if it doesn't


P.S :- Regarding $#my_array it is the index of the last element in the array "my_array"



Last edited by SaTaN; 09-01-2003 at 10:30 AM.
 
Old 09-01-2003, 10:31 AM   #8
ocularbob
Member
 
Registered: Nov 2002
Location: brooklyn NYC
Distribution: gentoo
Posts: 212

Original Poster
Rep: Reputation: 30
it aint pretty but it does work

open(DATFILE,"$DATFILE") || die "no read";
@DATA = <DATFILE>;
$N = 0;
foreach $DATA (@DATA) {
$N++;
}
close(DATFILE);
open(DATFILE,">$DATFILE") || die "no write";
flock(DATFILE, $LOCK_EXCLUSIVE);
$D = ($N-1);
$C = 0;
foreach $DATA (@DATA) {
while($C < $D){
print DATFILE "$DATA[$C]";
$C++;
}
}
close(DATFILE);

thanks guys
 
Old 09-01-2003, 10:37 AM   #9
ocularbob
Member
 
Registered: Nov 2002
Location: brooklyn NYC
Distribution: gentoo
Posts: 212

Original Poster
Rep: Reputation: 30
the problem was that the script that i wrote to add lines to the file
was putting everything all on the same line. I'm a fool
what i had come up with originally to delete lines also works.
alls well that ends well.
later on
scott

Last edited by ocularbob; 09-01-2003 at 10:38 AM.
 
Old 09-01-2003, 10:50 AM   #10
SaTaN
Member
 
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223

Rep: Reputation: 33
open(DATFILE,">>file") || die "no dice";
flock(DATFILE, $LOCK_EXCLUSIVE);
@DATA = <DATFILE>;
$N = 0;
foreach $LINE (@DATA) {
$N = ++$N;
}
print $N;
truncate (DATFILE, "--$N");
close(DATFILE);


I just modified ur first post by adding a print statement for $N

The output is $N=0 Thatz why all the lines were being deleted....

So, I don't think the first code of your's works and the reason I suppose is what I have given in post 2
 
Old 09-01-2003, 10:59 AM   #11
ocularbob
Member
 
Registered: Nov 2002
Location: brooklyn NYC
Distribution: gentoo
Posts: 212

Original Poster
Rep: Reputation: 30
yeah i just came to the same conclusion.
I still have the problem that when i add to the text file from my script
everything winds up on the same line. here is the routine for adding to the file.

my $TEXT = param("itext");
open(DATFILE,">>$DATFILE") || ($MESG = "could not open $DATFILE");
flock(DATFILE, $LOCK_EXCLUSIVE);
print DATFILE '<p>';
print DATFILE "$TEXT";
print DATFILE '</p>';
$MESG = ' text added successfully';
close(DATFILE);

this is run from a form. and then the whole script is reloaded
then when i use the form again, it goes on the same line as the previous.
thanks alot for all you help. slowly starting to get a handle on all of this.
 
Old 09-01-2003, 11:06 AM   #12
SaTaN
Member
 
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223

Rep: Reputation: 33
Quote:
Originally posted by ocularbob

print DATFILE '</p>';
.
Instead of this you can have

print DATFILE "</p>\n";

So that "\n" i.e, a newline character is also written on to the file

The next time you call the form It'll go on to the next line automatically..

P.S :- I assumed that this line is the last line that is being
written on to the file at one time ...

Last edited by SaTaN; 09-01-2003 at 11:08 AM.
 
Old 09-01-2003, 11:13 AM   #13
ocularbob
Member
 
Registered: Nov 2002
Location: brooklyn NYC
Distribution: gentoo
Posts: 212

Original Poster
Rep: Reputation: 30
your assuption is correct.
now i guess i'll have to figure out how to use
chop() so that when i read the file into my HTML i don't see the
"\n"?
thanks a million
 
Old 09-01-2003, 11:19 AM   #14
SaTaN
Member
 
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223

Rep: Reputation: 33
I don't see any reason why you should use "chop" ....

<html><p>SaTaN</p></html>

and

<html><p>SaTaN</p>
</html>


both give the same page as output ......

I am sorry if I have understood the previous post wrong ....
 
Old 09-01-2003, 11:22 AM   #15
ocularbob
Member
 
Registered: Nov 2002
Location: brooklyn NYC
Distribution: gentoo
Posts: 212

Original Poster
Rep: Reputation: 30
seems i don't need chop() at all.
works perfectly
this is great.
I think that spells the end of this thread.

thanks again.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl - Tpl file - Need to replace new line character. knnirmal Programming 2 09-07-2004 02:27 PM
PERL editing single line in file with regex indiescene Programming 0 04-14-2004 08:18 AM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 08:09 PM
How to remove line of text from file netkepala Linux - General 2 05-23-2003 11:49 AM
perl: replacing a special line in a file markus1982 Programming 1 09-26-2002 02:05 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:07 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration