473,324 Members | 2,214 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Removing new line character from a string

Chittaranjan
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
8 41721
numberwhun
3,509 Expert Mod 2GB
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
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
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
4,059 Expert 2GB
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
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
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
4,059 Expert 2GB
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
$test =~ s/\n/ /g;

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

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

Similar topics

3
by: Yazar Yolait | last post by:
I want to skip lines in a file that are blank and that start with "&". So I strip(None) them and then startswith("&") but the only problem is if the line has nothing but white space and I...
4
by: Surya Kiran | last post by:
Hi all, I'm facing a wierd problem. I've a file, which is getting updated every now and then. and i'm having another program, which monitors the file. I've to read the file line by line, and in...
4
by: Someonekicked | last post by:
if you create a new file , open it and only hit enter then save. if you use seekg(0,ios::end) and then tellg(), tellg() will return 2 (not 1); so new line character is considered as two characters....
10
by: Christopher Lusardi | last post by:
How would I put an end of line character in the second line below? Textbox1.Text = "Hello " Textbox1.Text = Textbox1.Text = "World" Thanks, Chris Lusardi
8
by: Brand Bogard | last post by:
Does the C standard include a library function to convert an 8 bit character string to a 16 bit character string?
6
by: bruce | last post by:
hi... i'm running into a problem where i'm seeing non-ascii chars in the parsing i'm doing. in looking through various docs, i can't find functions to remove/restrict strings to valid ascii...
1
by: KK | last post by:
Dear comp.lang.c++, The code below reads off strings of a file into a vector. ifstream srcfile("c:\\test.txt"); istream_iterator<stringstrIter(srcfile), eos; vector<string strvec(strIter, eos);...
5
by: LEM | last post by:
Hi, I'm trying to remove any empty lines from a string, and I am doing the following: String pp; pp = "\r\n\r\n1\r\n23\r\n\r\n4"; pp = pp.Replace("\r\n\r\n", "\r\n");
0
by: dougancil | last post by:
I have the following code: Imports System.Data.SqlClient Public Class Main Protected WithEvents DataGridView1 As DataGridView Dim instForm2 As New Exceptions Private Sub...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.