[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl-AnyPerl] Converting CR's LF's etc.



At 20.08 -0400 1999.04.09, jason white wrote:
>Pretty low level question here...
>Any one got a good pointer on how to convert Carriage returns/linefeeds
>etc.  I just need to be able to capture them and then I'll convert them
>into <br> tags .  Basically I want it to take the cr , lf, or crlf and make
>it  like the user put it in (if they put a hard return).  I know the ascii
>values are 10 and 13.  I've done this with other scripting languages
>(ToolBook, HyperCard etc.) but not in Perl yet.  Is it just a matter of
>converting Hex values?

Well, it depends.  The basic answer is:

  $text =~ s/\015/\012/g;  # i like octals :)
  $text =~ s/\x0D/\x0A/g;  # same thing

But really, if you need more than that, I would need to know exactly what
you want to do, like:

* change CR to LF
* change CR or CRLF to LF
* change LF or CRLF to CR
* change any of CR, LF, or CRLF to local newline

etc.  I imagine you might want the latter, and it gets to be more
difficult, because first you have to identify what the newline characters
are.  I would do something like:

  my($nl) = ($text =~ /(\015?\012|\015)$/);  # get which newline used
  $text =~ s/$nl/\n/g;  # change all "newlines" to local newline, \n

--
Chris Nandor          mailto:pudge@pobox.com         http://pudge.net/
%PGPKey = ('B76E72AD', [1024, '0824090B CE73CA10  1FF77F13 8180B6B6'])

==== Want to unsubscribe from this list?
==== Send mail with body "unsubscribe" to macperl-anyperl-request@macperl.org