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: Beginner:
delete a new line character at the end of a string

 



ajr121
Novice

Oct 30, 2007, 4:34 PM

Post #1 of 11 (966 views)
delete a new line character at the end of a string Can't Post

I need to be able to delete a new line character only if it exists at the end of a string. New line characters within the string should not be deleted.

The posts that I have seen, replace /\n/ by // globally but I only need the last /n to be deleted.

Hope you can help?


KevinR
Veteran


Oct 30, 2007, 4:45 PM

Post #2 of 11 (964 views)
Re: [ajr121] delete a new line character at the end of a string [In reply to] Can't Post

use the chomp() function.

my $string = "this is\na test\n";
chomp($string);
print $string;
-------------------------------------------------


ajr121
Novice

Oct 30, 2007, 5:04 PM

Post #3 of 11 (962 views)
Re: [KevinR] delete a new line character at the end of a string [In reply to] Can't Post

Thanks

I had :-

$strg = "abc\nefg\n";

$strg = chomp($strg);

print $strg;

This gave 1 not abc\nefg


winfinit
User

Oct 30, 2007, 10:10 PM

Post #4 of 11 (948 views)
Re: [ajr121] delete a new line character at the end of a string [In reply to] Can't Post

winfinit:~ winfinit$ cat test
#!/usr/bin/perl
chomp($test="test\ntest\n");
print $test;

winfinit:~ winfinit$ perl test
test
testwinfinit:~ winfinit$

as you see it stripped last \n only not the one in the middle


winfinit
User

Oct 30, 2007, 10:13 PM

Post #5 of 11 (948 views)
Re: [ajr121] delete a new line character at the end of a string [In reply to] Can't Post


In Reply To
Thanks

I had :-

$strg = "abc\nefg\n";

$strg = chomp($strg); #this is incorrect, you dont have to reassign anything, just use chomp($strg); and that will place processed result back to your $strg variable

print $strg;

This gave 1 not abc\nefg


$strg = chomp($strg); #this is incorrect, you dont have to reassign anything, just use chomp($strg); and that will place processed result back to your $strg variable


ajr121
Novice

Oct 31, 2007, 2:21 AM

Post #6 of 11 (943 views)
Re: [winfinit] delete a new line character at the end of a string [In reply to] Can't Post

Great - Thanks - That worked.

I don't know whether this is outside the scope of forum but the reason why I was 'chomping' the new line character was because I am writing the string to a flat text file on Unix with :-

$L2filename = "calast2.txt";

# say $comments contains "abc\nedf\n"

chomp $ucomments;
open (SAV, ">$L2filename") || die "file no good";
print SAV "$ucomments";
close(SAV);

# calast2.txt should now contain "abc\nedf" - without the trailing \n

Later I am using the contents of the same flat file to populate a textarea in an html form :-

print <<YYY;
<textarea rows="16" name="fComments" cols="89" style="font-family: Arial">
YYY

open (TXT, "<$L2filename") || die "file no good";
while (<TXT>) {
chomp $_; # This chomp gets rid of return characters that seem to be intoduced in html
print $_;
}
close(TXT);

The trouble is that this always results in an extra new line (or return) character at the end of the text in the textarea even though I know that the text in the text file being used to save the data does not have a terminal new line character. The original chomp was designed to solve this but it does not help.

Any further ideas? - I think this will be of interest to all those people who have to manipulate data between text files and html forms.


KevinR
Veteran


Oct 31, 2007, 8:28 AM

Post #7 of 11 (930 views)
Re: [ajr121] delete a new line character at the end of a string [In reply to] Can't Post

You may be introducing the newline in your script when printing the textarea tag. Print it all on one line:

<textarea [attributes]>data</textarea>


Code
open (TXT, "<$L2filename") || die "file no good"; 
my $data = do{local $/; <TXT>}; #<-- slurp mode
close(TXT);

print qq{<textarea rows="16" name="fComments" cols="89" style="font-family: Arial">$data</textarea>\n};


Typically you don't have embedded newlines in a flat file, they are always only on the end of a line. Each browser may also treat the data sent via a textarea widget differently. Some may add a newline at each line break and some may add carraige returns and newlines. Unless there is a reason to retain the formatting you should be deleting the newlines and or carraige returns before writing the data to disk. You may be able to control how the data is sent using a textarea tag attribute like "nowrap" but there is no gaurantee and html can be edited by the user so it's best to do it all server side.
-------------------------------------------------


KevinR
Veteran


Oct 31, 2007, 8:35 AM

Post #8 of 11 (929 views)
Re: [ajr121] delete a new line character at the end of a string [In reply to] Can't Post

if theres any chance that html code is in the flat file, the textarea tag can be broken by the user inserting </textarea> into the textarea and saving the data to disk. You should escape all tags brackets before displaying them in the textarea tag to avoid this if necessary.
-------------------------------------------------


ajr121
Novice

Oct 31, 2007, 11:38 AM

Post #9 of 11 (923 views)
Re: [KevinR] delete a new line character at the end of a string [In reply to] Can't Post

There could be html code in the flat file such as <b> text </b> but I control this so we should not end up with any unescaped html tags being entered by User.

Thanks again.


ajr121
Novice

Oct 31, 2007, 11:40 AM

Post #10 of 11 (922 views)
Re: [KevinR] delete a new line character at the end of a string [In reply to] Can't Post

Thanks - will try this later this evening and report back.


ajr121
Novice

Oct 31, 2007, 4:44 PM

Post #11 of 11 (917 views)
Re: [KevinR] delete a new line character at the end of a string [In reply to] Can't Post

Yep - That code worked fine thanks, it got rid of terminal \n character.

I did add one more line of code to get rid of any intermediate new lines :-

open (TXT, "<$L2filename") || die "file no good";
my $data = do{local $/; <TXT>}; #<-- slurp mode
close(TXT);

$data =~ s/\n//g;

print qq{<textarea rows="16" name="fComments" cols="89" style="font-family: Arial">$data</textarea>\n};

 
 


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

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