James Edward Gray II wrote:
> I have a problem I just cant seem to get my head around, so any help is
> appreciated.
>
> I have a string. It could contain anything a Perl string can contain.
> I have to print this string to a file and later bring it back in
> exactly as it was. However, because of the file format, the string in
> the file may not contain \n characters. That's the only difference
> between the two representations of this string.
>
> Okay, obviously I need to replace all \n characters. Let's say I want
> to follow Perl's example and use a literal \ followed by a literal n.
> Then I would also need to escape \ characters. Okay, again we'll use
> Perl's \ and another \. Does that cover everything if \n is the only
> illegal character in my file format? I believe, so, but please correct
> me if I'm wrong.
>
> The to file conversion seems simple given the above:
>
> $string =~ s/\\/\\\\/g;
> $string =~ s/\n/\\n/g;
>
> Does that work as good as I think it does?
see if the following works better:
#!/usr/bin/perl -w
use strict;
my $o = my $s = "Funky \"St\nring\"\\\n\n\\\\\n\n\\\\\\\\\n\n\n\n\\\\";
$s =~ s.\n.\\n.g;
$s =~ s.\\(?!n).\\\\.g;
print "After encode: $s\n";
$s =~ s.\\n.\n.g;
$s =~ s.\\\\.\\.g;
print "they are ",$o eq $s ? "":"not ","same\n";
__END__
prints:
After encode: Funky "St\nring"\\\n\n\\\\\n\n\\\\\\\\\n\n\n\n\\\\
they are same
if the regx doesn't cover all cases, please post a few exceptions.
david
--
sub'_{print"@_ ";* \ = * __ ,\ & \}
sub'__{print"@_ ";* \ = * ___ ,\ & \}
sub'___{print"@_ ";* \ = * ____ ,\ & \}
sub'____{print"@_,\n"}&{_+Just}(another)->(Perl)->(Hacker)
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>