Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > How to expand escape sequence (e.g. \n)?

Reply
Thread Tools

How to expand escape sequence (e.g. \n)?

 
 
Wojtek Dziegielewski
Guest
Posts: n/a
 
      09-11-2004
How to expand escape sequences contained in a variable?


Example, if I want the input record separator ($/) to be 2 consecutive new
line characters, I can use literal string like this:

$/ = "/n/n";

However, my sequence for the input record separator is inside a variable,
like this:

my $a = '/n/n';

I need to assign the contents of $a to $/ in such a way that /n's will get
translated into newline characters. How can I have it accomplished in Perl?



 
Reply With Quote
 
 
 
 
Scott W Gifford
Guest
Posts: n/a
 
      09-11-2004
"Wojtek Dziegielewski" <> writes:

[...]

> my sequence for the input record separator is inside a variable,
> like this:
>
> my $a = '/n/n';
>
> I need to assign the contents of $a to $/ in such a way that /n's will get
> translated into newline characters. How can I have it accomplished in Perl?


It works like you'd expect, except there are two bugs in your code
above. First, you use a backslash, not a forward slash, to write \n.
Second, \n isn't interpreted inside of single quotes, but only inside
of double quotes. So if you do:

my $a = "\n\n";
$/ = $a;

you'll get what you expect.

----ScottG.
 
Reply With Quote
 
 
 
 
Wojtek Dziegielewski
Guest
Posts: n/a
 
      09-11-2004
My apologies for the typo - it was a long day for me.

My actual scenario however is more complex and cannot be addressed by a
simple assignment. I used initial assignment to $a as an illustration and
not the actual piece of code.

my $a = '\n\n'; # in reality $a is read from XML config file, so I can't
say my $a = "\n\n"; instead

I need to take a value of $a (which contains literal '\n\n') and have it
translated to 2 newlines. Haw can I do that?

"Scott W Gifford" <> wrote in message
news:.. .
> "Wojtek Dziegielewski" <> writes:
>
> [...]
>
> > my sequence for the input record separator is inside a variable,
> > like this:
> >
> > my $a = '/n/n';
> >
> > I need to assign the contents of $a to $/ in such a way that /n's will

get
> > translated into newline characters. How can I have it accomplished in

Perl?
>
> It works like you'd expect, except there are two bugs in your code
> above. First, you use a backslash, not a forward slash, to write \n.
> Second, \n isn't interpreted inside of single quotes, but only inside
> of double quotes. So if you do:
>
> my $a = "\n\n";
> $/ = $a;
>
> you'll get what you expect.
>
> ----ScottG.



 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      09-11-2004
Wojtek Dziegielewski wrote:
> My apologies for the typo - it was a long day for me.
>
> My actual scenario however is more complex and cannot be addressed by a
> simple assignment. I used initial assignment to $a as an illustration and
> not the actual piece of code.
>
> my $a = '\n\n'; # in reality $a is read from XML config file, so I can't
> say my $a = "\n\n"; instead
>
> I need to take a value of $a (which contains literal '\n\n') and have it
> translated to 2 newlines. Haw can I do that?
>


A simple search and replace will do it:

$a =~ s/\\n/\n/g;

That searched for all literal \n and replaces them with a new-line
character.

Paul Lalli
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      09-11-2004

[ Please do not post upside-down. We don't like it here (nor does
anywhere else on Usenet).
]


Wojtek Dziegielewski <> wrote:

> I need to take a value of $a (which contains literal '\n\n') and have it
> translated to 2 newlines. Haw can I do that?



$a =~ s/\\n/\n/g;


--
Tad McClellan SGML consulting
Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      09-11-2004
Wojtek Dziegielewski top-posts (please don't):

>
> my $a = '\n\n'; # in reality $a is read from XML config file, so I can't
> say my $a = "\n\n"; instead


You could condider using the actual string in the XML rather than a
representation of it.

> I need to take a value of $a (which contains literal '\n\n') and have it
> translated to 2 newlines. Haw can I do that?


Take a step back. The string '\n\n' is a representation of two newlines
in Perl source code. There is a way to cause perl to interpret a
string as Perl source code. This is the eval(STRING) function. The
problem is that if you use the eval(STRING) to implement this you allow
the the person writing the config file to execute arbitrary commands
inclusing stuff like system('rm -rf /'). For this reason many of the
people 'round here consider the existance of eval() to be forbidden
knowledge.

Depending on circumstances it may be that the config file already has
the capability to execute arbitrary code. For example there may be
parameters that are to be interpreted as shell commands. If this is the
case then then perhaps eval() would not be such a bad idea.

So my advice is probably that you should not use eval() but you should
not make this decision by default simply by not being aware of the option.

Should you want to use eval...

chop( $a = eval "<<__EOD__\n$a\n__EOD__" );

This will expand \n and \t and anthing else you could embeb in a
double-quotish context like \x{263a} but also including arbitrary Perl
statements using @{ whatever }.

If you do not want to provide the config file with the full semantics of
Perl double-quotish string then you can implement a subset of the
semantics using a s/// as others have suggested - just choose your subset.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How to read strings cantaining escape character from a file and useit as escape sequences? slomo Python 5 12-02-2007 11:39 AM
escape sequence branigan Cisco 4 04-15-2005 02:14 PM
escape sequence \n marcia ASP .Net 4 08-28-2003 07:16 PM
Escape Sequence to printer from Web Page h babu ASP .Net 1 07-16-2003 08:08 PM



Advertisments