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

Discussion in 'Perl Misc' started by Wojtek Dziegielewski, Sep 11, 2004.

  1. 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?
     
    Wojtek Dziegielewski, Sep 11, 2004
    #1
    1. Advertising

  2. "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.
     
    Scott W Gifford, Sep 11, 2004
    #2
    1. Advertising

  3. 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.
     
    Wojtek Dziegielewski, Sep 11, 2004
    #3
  4. Wojtek Dziegielewski

    Paul Lalli Guest

    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
     
    Paul Lalli, Sep 11, 2004
    #4
  5. [ 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
     
    Tad McClellan, Sep 11, 2004
    #5
  6. 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.
     
    Brian McCauley, Sep 11, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. h babu
    Replies:
    1
    Views:
    415
    Vidar Petursson
    Jul 16, 2003
  2. marcia

    escape sequence \n

    marcia, Aug 28, 2003, in forum: ASP .Net
    Replies:
    4
    Views:
    558
    marcia
    Aug 28, 2003
  3. Jeff

    Unrecognized escape sequence

    Jeff, Jun 30, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    39,084
    mansoorbhai
    Jun 21, 2011
  4. N U

    escape sequence

    N U, Sep 12, 2003, in forum: C Programming
    Replies:
    7
    Views:
    560
    LibraryUser
    Sep 13, 2003
  5. slomo
    Replies:
    5
    Views:
    1,575
    Duncan Booth
    Dec 2, 2007
Loading...

Share This Page