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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
|
08-31-2003, 04:35 PM
|
#1
|
Member
Registered: Nov 2002
Location: brooklyn NYC
Distribution: gentoo
Posts: 212
Rep:
|
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
|
|
|
08-31-2003, 11:08 PM
|
#2
|
Member
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223
Rep:
|
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.
|
|
|
08-31-2003, 11:57 PM
|
#3
|
Member
Registered: Nov 2002
Location: brooklyn NYC
Distribution: gentoo
Posts: 212
Original Poster
Rep:
|
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
|
|
|
09-01-2003, 12:05 AM
|
#4
|
Member
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223
Rep:
|
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.
|
|
|
09-01-2003, 09:11 AM
|
#5
|
Member
Registered: Jun 2003
Location: Toronto, Ontario, Canada
Posts: 288
Rep:
|
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.
|
|
|
09-01-2003, 10:14 AM
|
#6
|
Member
Registered: Nov 2002
Location: brooklyn NYC
Distribution: gentoo
Posts: 212
Original Poster
Rep:
|
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.
|
|
|
09-01-2003, 10:22 AM
|
#7
|
Member
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223
Rep:
|
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.
|
|
|
09-01-2003, 10:31 AM
|
#8
|
Member
Registered: Nov 2002
Location: brooklyn NYC
Distribution: gentoo
Posts: 212
Original Poster
Rep:
|
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
|
|
|
09-01-2003, 10:37 AM
|
#9
|
Member
Registered: Nov 2002
Location: brooklyn NYC
Distribution: gentoo
Posts: 212
Original Poster
Rep:
|
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.
|
|
|
09-01-2003, 10:50 AM
|
#10
|
Member
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223
Rep:
|
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
|
|
|
09-01-2003, 10:59 AM
|
#11
|
Member
Registered: Nov 2002
Location: brooklyn NYC
Distribution: gentoo
Posts: 212
Original Poster
Rep:
|
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.
|
|
|
09-01-2003, 11:06 AM
|
#12
|
Member
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223
Rep:
|
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.
|
|
|
09-01-2003, 11:13 AM
|
#13
|
Member
Registered: Nov 2002
Location: brooklyn NYC
Distribution: gentoo
Posts: 212
Original Poster
Rep:
|
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
|
|
|
09-01-2003, 11:19 AM
|
#14
|
Member
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223
Rep:
|
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 ....
|
|
|
09-01-2003, 11:22 AM
|
#15
|
Member
Registered: Nov 2002
Location: brooklyn NYC
Distribution: gentoo
Posts: 212
Original Poster
Rep:
|
seems i don't need chop() at all.
works perfectly
this is great.
I think that spells the end of this thread.
thanks again.
|
|
|
All times are GMT -5. The time now is 03:07 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|