Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Removing the ^M character, but also removing the newline

by c (Hermit)
on Dec 29, 2001 at 04:51 UTC ( [id://135026]=perlquestion: print w/replies, xml ) Need Help??

c has asked for the wisdom of the Perl Monks concerning the following question:

all- i have an html form that passes information within a <textarea> as part of its feedback. the type within the text area can obviously include hard returns sent by the page visitor. these appear as ^M when i print the data to a flat file. i would like to remove the entire carriage return, however and not just the ^M character.

i perused this but found that using tr/// or s/// simply removes the character, and doesnt really get rid of the newline. a post within the one i mentioned seemed to touch on this, but no one seemed to follow up on the thread.

currently, i have:

my @formfields = $query->param; for my $i(@formfields) { my $j = lc $i; $formdata{$j} = $query->param("$i"); $formdata{$j} =~ s/\cM//g; }

humbly -c

Replies are listed 'Best First'.
You may be making an error.
by metadoktor (Hermit) on Dec 29, 2001 at 05:32 UTC
    I have no idea what you are doing wrong but this works.

    #!/usr/local/bin/perl -w
    use strict;
    
    my ($str);
    
    $_="what\n is going\non here?\n";
    
    print "Before $_\n";
    
    s/\n//g;
    
    print "After $_\n";
    

    It prints out the following:

    Before what 
    is going 
    on here?
    
    After what is goingon here?
    
    

    There is no problem. Double check your code. :)

    metadoktor

    "The doktor is in."

      yes. it looks like i was missing a substitution for \n in my original code. i was only replacing the ^M character, but leaving the new lines in place.

      thanks -c

Re: Removing the ^M character, but also removing the newline
by ehdonhon (Curate) on Dec 29, 2001 at 05:11 UTC

    There is additional discussion of this issue here.

Re: Removing the ^M character, but also removing the newline
by atcroft (Abbot) on Dec 29, 2001 at 22:56 UTC

    Wouldn't you be able to do something like $formdata{$j} =~ s/[\n\r]+//gs; ?

    Am I correct that this would

    • treat the entire string as one line for purposes of matching,
    • perform a global replace on one or more of "\n" (new line, aka line feed) and/or "\r" (return, aka carriage return), and
    • replace them with nothing,
    thus effectively removing them (as desired)? (You could also replace with a single space or something else, if preferred.)

    (The other solutions presented, however, may be more correct and/or efficient, however-this was just what I threw together while reading the post. I tried testing against files I generated with lines ending in "\n", "\r", "\r\n", and "\n\r", and it appeared to work, but YMMV.)

Re: Removing the ^M character, but also removing the newline
by ton (Friar) on Dec 29, 2001 at 04:56 UTC
    try chomp
    -ton
    -----
    Be bloody, bold, and resolute; laugh to scorn
    The power of man...
      chomp is only going to remove the trailing newline character of the entire line. my issue is that i may have multiple new lines within my hash value, so chomp won't do it for me.

      -c

        Tell chomp what your newline is like this:

        local $/ = "\r\n"; chomp;

        Chomp chomps whatever you tell it a newline is. local localizes that setting so it's automagically replaced with the standard newline for your system when the block the local setting is within is done.

        Name another language that makes stripping newlines this easy. 8-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://135026]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2024-03-28 15:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found