How to convert between a string w/ backslashes and a string w/special characters?

Discussion in 'Perl Misc' started by Peng Yu, Jul 11, 2010.

  1. Peng Yu

    Peng Yu Guest

    Suppose I have a string $x='\t\n', I want to convert it to $y="\t\n".
    I also want to convert $y back to $x. I have googled. But I haven''t
    any related function, maybe because I didn't use the right search
    keywords. Would you please let me know what functions I can use to do
    the conversions?
     
    Peng Yu, Jul 11, 2010
    #1
    1. Advertising

  2. On 2010-07-11 04:55, Tad McClellan <> wrote:
    > Peng Yu <> wrote:
    >> Suppose I have a string $x='\t\n', I want to convert it to $y="\t\n".

    >
    > --------------------
    > #!/usr/bin/perl
    > use warnings;
    > use strict;
    >
    > $_ = q($x='\t\n');
    > tr/x'/y"/;
    > print "$_\n";
    > --------------------


    From the posting guidelines:

    | Speak Perl rather than English, when possible
    | Perl is much more precise than natural language. Saying it in Perl
    | instead will avoid misunderstanding your question or problem.
    |
    | Do not say: I have variable with "foo\tbar" in it.
    |
    | Instead say: I have $var = "foo\tbar", or I have $var = 'foo\tbar',
    | or I have $var = <DATA> (and show the data line).

    I think Yu was folling your advice here.

    hp
     
    Peter J. Holzer, Jul 11, 2010
    #2
    1. Advertising

  3. Peng Yu

    Guest

    Re: How to convert between a string w/ backslashes and a string w/ special characters?

    On Sat, 10 Jul 2010 23:55:55 -0500, Tad McClellan <> wrote:

    >Peng Yu <> wrote:
    >
    >> Suppose I have a string $x='\t\n', I want to convert it to $y="\t\n".

    >
    >--------------------
    >#!/usr/bin/perl
    >use warnings;
    >use strict;
    >
    >$_ = q($x='\t\n');
    >tr/x'/y"/;
    >print "$_\n";
    >--------------------


    He has a string, it has a known value, its a constant.
    Any conversion can't be treated programatically if the
    new value is known and constant. Therefore, transliterate
    is overkill, conceptually wrong, and slow.

    The only answer is:

    $string = q($x='\t\n');
    $string = q($y="\t\n");

    -sln
     
    , Jul 11, 2010
    #3
  4. Peng Yu

    C.DeRykus Guest

    On Jul 10, 4:48 pm, Peng Yu <> wrote:
    > Suppose I have a string $x='\t\n', I want to convert it to $y="\t\n".
    > I also want to convert $y back to $x. I have googled. But I haven''t
    > any related function, maybe because I didn't use the right search
    > keywords. Would you please let me know what functions I can use to do
    > the conversions?


    eval qq{\$y = "$x"; 1} or die $@;

    But, I wouldn't necessarily recommend this.
    String eval is slow and needs safeguards if
    there's user input. See perldoc -f eval and
    perldoc perlsec

    --
    Charles DeRykus
     
    C.DeRykus, Jul 13, 2010
    #4
    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. Replies:
    5
    Views:
    4,906
    Chris Head
    Sep 2, 2005
  2. Stefan Mueller
    Replies:
    3
    Views:
    33,318
    Stefan Mueller
    Jul 23, 2006
  3. Replies:
    2
    Views:
    1,147
    Ingo Menger
    May 31, 2007
  4. rvino
    Replies:
    0
    Views:
    4,723
    rvino
    Aug 14, 2007
  5. majna
    Replies:
    4
    Views:
    790
    Thomas 'PointedEars' Lahn
    Sep 19, 2007
Loading...

Share This Page