By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,854 Members | 849 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,854 IT Pros & Developers. It's quick & easy.

Removing new line character from a string

Chittaranjan
P: 51
Hi All,

I am using perl to get the data from the form fields and I have a text area from where if some one enters data like below:

"This is test
And this is also test
This is also test."

So here you can notify the text is entered with hitting of enter key to have the line break. But I am having the problem while saving the data entered in this field in a text file then there also it is saving with these line breaks(new lines) so if am trying to upload the file to the fields then it is saying that invalid file.

So I was thinking is there a way in perl to remove the new line character from the string while saving the form data. So that I will not have the problem while uploading.

Please let me know with some sample examples to solve this I will be waiting for all your valuable responses.

Thanks,
Chittaranjan :)
Oct 26 '07 #1
Share this Question
Share on Google+
8 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
Hi All,

I am using perl to get the data from the form fields and I have a text area from where if some one enters data like below:

"This is test
And this is also test
This is also test."

So here you can notify the text is entered with hitting of enter key to have the line break. But I am having the problem while saving the data entered in this field in a text file then there also it is saving with these line breaks(new lines) so if am trying to upload the file to the fields then it is saying that invalid file.

So I was thinking is there a way in perl to remove the new line character from the string while saving the form data. So that I will not have the problem while uploading.

Please let me know with some sample examples to solve this I will be waiting for all your valuable responses.

Thanks,
Chittaranjan :)
You will want to read up on the chomp() function. That does what you want.

Regards,

Jeff
Oct 26 '07 #2

P: 4
I have a simpler Perl question, but chomp doesn't seem to be doing the trick.

I've input a file in paragraph mode (ie set $/="") as much of the data i need to isolate is multi-line. I have set up a while (<>) loop, in which I have several if statements to pattern match. I am saving long text strings into a hash but I need the values not to have any embedded "/n", such as:

Justice Hudson did not participate in the consideration or decision of\n
this case.

I need the entire sentence (ie the entire string) as a value in a hash with no embedded "\n". I can isolate the pattern fine, and I can assign this string to a hash value, but no matter what i do I can't remove that "\n" since it is embedded in a string (opposed to being at the end, which would be no problem to remove).

Any suggestions?

Thanks a bunch!

Michael
Nov 1 '07 #3

P: 4
I should also note that once i have isolated the entire string (with embedded "\n") to a hash value, say $case{'judges'} for example, and i try the following after the while (<>) loop:

if (exists $case{'judges'}){
$case{'judges'}=~s/\n//g;}

i get the following when i try to print $case{'judges'}:

Justice Hudthis case.t participate in the consideration or decision of


which just makes me sad. :-(
Any idea what i am doing wrong?
Thanks, again.
Michael
Nov 1 '07 #4

KevinADC
Expert 2.5K+
P: 4,059
There is something else going on. As you can see by this example, removing newlines is no problem:

Expand|Select|Wrap|Line Numbers
  1. my $test = qq{this
  2. is a
  3. test
  4. };
  5. print "before:\n";
  6. print $test;
  7. $test =~ s/\n/ /g;
  8. print "\nafter:\n";
  9. print $test;
Nov 1 '07 #5

P: 4
Thanks for your help but I still am apparently breaking Perl. I tried your test and yes, it works. But when I try to apply it to my script, all hell breaks lose. At its most basic, this is my script:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w
  2. my %case = (); # case hash
  3. my $judges = '';               # temp key
  4. open (IN, "/Users/MicWood/Documents/Perl/Opinion\ Scripts/output.txt");
  5. $/="";     # paragraph input mode
  6. while (<IN>) {
  7.     if (/^JUDGES:\s(.+)\n\n/s){
  8.         $case{'judges'}=$1};
  9.     next;}
  10. print "before:\n";
  11. print $case{'judges'};
  12. $case{'judges'}=~ s/\n/ /;
  13. print "\nafter:\n"; 
  14. print $case{'judges'};
  15. print "\n";
  16.  
The result between the two prompts:
before:
Justice HUDSON did not participate in the consideration or decision of
this case.
after:
this case.SON did not participate in the consideration or decision of



Why is it transposing the two parts of the string?

Thanks again!
Michael
Nov 1 '07 #6

P: 4
Whoops:
that should be:
$case{'judges'}=~ s/\n/ /g;

not
$case{'judges'}=~ s/\n/ /;


But same transposed result as before.

Thanks
Michael
Nov 1 '07 #7

KevinADC
Expert 2.5K+
P: 4,059
Maybe something do with the way you are printing the output while still in the loop, try like this:


Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w
  2. my %case = (); # case hash
  3. my $judges = '';               # temp key
  4. open (IN, "/Users/MicWood/Documents/Perl/Opinion\ Scripts/output.txt");
  5. $/="";     # paragraph input mode
  6. while (<IN>) {
  7.    if (/^JUDGES:\s(.+)\n\n/s) {
  8.       $case{'judges'}=$1;
  9.       last;
  10. }
  11. close (IN);
  12. print "before:\n";
  13. print $case{'judges'};
  14. $case{'judges'}=~ s/\n/ /;
  15. print "\nafter:\n"; 
  16. print $case{'judges'};
  17. print "\n";
  18.  
If that does not help, post the input file or attach it to a post.
Nov 2 '07 #8

P: 1
$test =~ s/\n/ /g;

can you explain this is this find and replace kind of thing
Jul 16 '15 #9

Post your reply

Sign in to post your reply or Sign up for a free account.