substituting $ to \$ in a var

Discussion in 'Perl Misc' started by dmedhora@gmail.com, Mar 21, 2006.

  1. Guest

    Hi

    I have a var that has this value:

    $var="$logfile=x.$$.log";

    If I print $var, I get

    $logfile=x.$$.log

    But If I want to change the $ to \$ in the value of the variable
    that is I want $var to have \$logfile=x.\$\$.log as its value then how
    do I do it?

    1 while ($var =~ s/\$/\\\$/g); does not work :(

    Any help would be appreciated, thanks!
     
    , Mar 21, 2006
    #1
    1. Advertising

  2. wrote:
    > I have a var that has this value:
    >
    > $var="$logfile=x.$$.log";
    >
    > If I print $var, I get
    >
    > $logfile=x.$$.log
    >
    > But If I want to change the $ to \$ in the value of the variable
    > that is I want $var to have \$logfile=x.\$\$.log as its value


    Why would you want that?

    > then how do I do it?
    >
    > 1 while ($var =~ s/\$/\\\$/g); does not work :(


    Skip the while loop.

    $var =~ s/\$/\\\$/g;

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Mar 21, 2006
    #2
    1. Advertising

  3. <> wrote:
    > Hi
    >
    > I have a var that has this value:
    >
    > $var="$logfile=x.$$.log";
    >
    > If I print $var, I get
    >
    > $logfile=x.$$.log



    I don't get that.

    perl -e '$var="$logfile=x.$$.log"; print $var'

    makes output that does not look like what you (say you) are getting.


    If I run code different from what you claim to have run

    perl -e '$var=q($logfile=x.$$.log); print $var'

    then I _do_ get the output you claim.


    So, either the code you gave us in not the real code that you used,
    or the output is not as you claim.

    Which is it?


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Mar 21, 2006
    #3
  4. Tad McClellan wrote:
    > <> wrote:
    >>I have a var that has this value:
    >>
    >>$var="$logfile=x.$$.log";
    >>
    >>If I print $var, I get
    >>
    >>$logfile=x.$$.log

    >
    > If I run code different from what you claim to have run


    The OP didn't really claim to have run the first line, did he? He rather
    showed the string that $var contained, which should be obvious since he
    told us what printing the variable outputs.

    > So, either the code you gave us in not the real code that you used,
    > or the output is not as you claim.
    >
    > Which is it?


    If you want to say that he shouldn't have used a format that looks like
    a Perl assignment when explaining the contents of $var, why don't you
    just say so?

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Mar 21, 2006
    #4
  5. Gunnar Hjalmarsson <> wrote in
    news::

    > Tad McClellan wrote:
    >> <> wrote:
    >>>I have a var that has this value:
    >>>
    >>>$var="$logfile=x.$$.log";
    >>>
    >>>If I print $var, I get
    >>>
    >>>$logfile=x.$$.log

    >>
    >> If I run code different from what you claim to have run

    >
    > The OP didn't really claim to have run the first line, did he?


    The OP plainly showed an assignment.

    On the other hand, this post quite effectively makes the case that it is
    always best to post code that can be tried out by others.

    I still can't figure out what the heck is going on.

    I think the OP is trying to pass arguments to some program, and is
    trying to avoid shell expansion.

    In which case, escaping is not the best solution.

    Because I was not able to figure out what the OP was trying to do, I
    passed on this one.

    Sinan
    --
    A. Sinan Unur <>
    (remove .invalid and reverse each component for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Mar 21, 2006
    #5
  6. Gunnar Hjalmarsson <> wrote:
    > Tad McClellan wrote:
    >> <> wrote:
    >>>I have a var that has this value:
    >>>
    >>>$var="$logfile=x.$$.log";
    >>>
    >>>If I print $var, I get
    >>>
    >>>$logfile=x.$$.log

    >>
    >> If I run code different from what you claim to have run

    >
    > The OP didn't really claim to have run the first line, did he? He rather
    > showed the string that $var contained,



    He showed a variable, and an assignment and a semi-colon,
    none of which were the contents of any variable.

    I therefore, assumed that it was meant to be Perl code.


    > which should be obvious since he
    > told us what printing the variable outputs.



    When he tells us output we should presume that he is being literal,
    and when he tells us variable contents we should presume that
    he is being figurative?

    Why is it that we should do that?


    >> So, either the code you gave us in not the real code that you used,
    >> or the output is not as you claim.
    >>
    >> Which is it?

    >
    > If you want to say that he shouldn't have used a format that looks like
    > a Perl assignment when explaining the contents of $var, why don't you
    > just say so?



    Because being told about the problems that that can cause is
    not as memorable as experiencing those problems firsthand.

    Things easily learned are easily forgotton. Things that are harder
    to learn are harder to forget.

    I chose to show the problems rather than simply state the problems.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Mar 21, 2006
    #6
  7. robic0 Guest

    On Tue, 21 Mar 2006 15:55:08 -0600, Tad McClellan <> wrote:

    >Gunnar Hjalmarsson <> wrote:
    >> Tad McClellan wrote:
    >>> <> wrote:
    >>>>I have a var that has this value:
    >>>>
    >>>>$var="$logfile=x.$$.log";
    >>>>
    >>>>If I print $var, I get
    >>>>
    >>>>$logfile=x.$$.log
    >>>
    >>> If I run code different from what you claim to have run

    >>
    >> The OP didn't really claim to have run the first line, did he? He rather
    >> showed the string that $var contained,

    >
    >
    >He showed a variable, and an assignment and a semi-colon,
    >none of which were the contents of any variable.
    >
    >I therefore, assumed that it was meant to be Perl code.
    >
    >
    >> which should be obvious since he
    >> told us what printing the variable outputs.

    >
    >
    >When he tells us output we should presume that he is being literal,
    >and when he tells us variable contents we should presume that
    >he is being figurative?
    >
    >Why is it that we should do that?
    >
    >
    >>> So, either the code you gave us in not the real code that you used,
    >>> or the output is not as you claim.
    >>>
    >>> Which is it?

    >>
    >> If you want to say that he shouldn't have used a format that looks like
    >> a Perl assignment when explaining the contents of $var, why don't you
    >> just say so?

    >
    >
    >Because being told about the problems that that can cause is
    >not as memorable as experiencing those problems firsthand.
    >
    >Things easily learned are easily forgotton. Things that are harder
    >to learn are harder to forget.
    >

    Tad, this is ZEN and very true. I will never forget this statement!

    >I chose to show the problems rather than simply state the problems.
     
    robic0, Mar 21, 2006
    #7
  8. Tad McClellan wrote:
    > Gunnar Hjalmarsson wrote:
    >>The OP didn't really claim to have run the first line, did he? He rather
    >>showed the string that $var contained,

    >
    > He showed a variable, and an assignment and a semi-colon,
    > none of which were the contents of any variable.


    True, but...

    >>which should be obvious since he
    >>told us what printing the variable outputs.

    >
    > When he tells us output we should presume that he is being literal,
    > and when he tells us variable contents we should presume that
    > he is being figurative?
    >
    > Why is it that we should do that?


    Because it's the only interpretation of his post that makes sense.

    >>If you want to say that he shouldn't have used a format that looks like
    >>a Perl assignment when explaining the contents of $var, why don't you
    >>just say so?

    >
    > Because being told about the problems that that can cause is
    > not as memorable as experiencing those problems firsthand.
    >
    > Things easily learned are easily forgotton. Things that are harder
    > to learn are harder to forget.
    >
    > I chose to show the problems rather than simply state the problems.


    That approach would have been justified in a private conversation with
    the OP. Now, since this is a high-traffic Usenet group, it merely
    contributes to the noise.

    IMO. ;-)

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Mar 21, 2006
    #8
  9. robic0 Guest

    On Tue, 21 Mar 2006 23:42:15 +0100, Gunnar Hjalmarsson <> wrote:

    >Tad McClellan wrote:
    >> Gunnar Hjalmarsson wrote:
    >>>The OP didn't really claim to have run the first line, did he? He rather
    >>>showed the string that $var contained,

    >>
    >> He showed a variable, and an assignment and a semi-colon,
    >> none of which were the contents of any variable.

    >
    >True, but...
    >
    >>>which should be obvious since he
    >>>told us what printing the variable outputs.

    >>
    >> When he tells us output we should presume that he is being literal,
    >> and when he tells us variable contents we should presume that
    >> he is being figurative?
    >>
    >> Why is it that we should do that?

    >
    >Because it's the only interpretation of his post that makes sense.
    >
    >>>If you want to say that he shouldn't have used a format that looks like
    >>>a Perl assignment when explaining the contents of $var, why don't you
    >>>just say so?

    >>
    >> Because being told about the problems that that can cause is
    >> not as memorable as experiencing those problems firsthand.
    >>
    >> Things easily learned are easily forgotton. Things that are harder
    >> to learn are harder to forget.
    >>


    <analysis breakdown>
    >> I chose to show the problems rather than simply state the problems.

    >

    is equivalent to this:
    >That approach would have been justified in a private conversation with
    >the OP. Now, since this is a high-traffic Usenet group, it merely
    >contributes to the noise.
    >
    >IMO. ;-)


    imo

    robic0
     
    robic0, Mar 21, 2006
    #9
  10. Gunnar Hjalmarsson <> wrote:
    > Tad McClellan wrote:
    >> Gunnar Hjalmarsson wrote:



    >>>If you want to say that he shouldn't have used a format that looks like
    >>>a Perl assignment when explaining the contents of $var, why don't you
    >>>just say so?

    >>
    >> Because being told about the problems that that can cause is
    >> not as memorable as experiencing those problems firsthand.
    >>
    >> Things easily learned are easily forgotton. Things that are harder
    >> to learn are harder to forget.
    >>
    >> I chose to show the problems rather than simply state the problems.

    >
    > That approach would have been justified in a private conversation with
    > the OP.



    I, obviously, felt that it was justified in the forum that
    I posted it to.


    > Now, since this is a high-traffic Usenet group, it merely
    > contributes to the noise.



    Now, since this is a high-traffic Usenet group, many many
    people will have seen firsthand the problems that ambiguous
    posts can generate.


    > IMO. ;-)



    I disagree with your opinions frequently.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Mar 22, 2006
    #10
  11. Guest

    Hi all, First of all let me apologize for being an idiot.

    This is what I wanted to do:

    1) #!/usr/bin/perl

    2) $var='$logfile="x.$$.log"';
    3) print "$var\n";

    4) $var=~s/\$/\\\$/g;

    5) print "$var\n";

    Line 2 is where I got it wrong, so the confusion.
    I should have used single quotes.

    I simply wanted a string which has $ signs to be replaced by \$
    Actually I wanted a before and after set of strings that I could use in
    a sed statement.
    I actually did implement the above "solution" to only 1 string ( by
    mistake, i.e I forgot to substitute
    BOTH before and after strings.) Thereafter when printing out the
    potential sed command,
    since I couldnt see the before and after strings substituted, I then
    got misled by
    Section 5.2.3.2: When a global substitution just isn't global enough. )
    all while under pressure.

    Apologies once again, Thanks.
     
    , Mar 22, 2006
    #11
    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. Alvin Bruney

    Threads.. Session var lost, App var ok

    Alvin Bruney, Dec 2, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    374
    rooster575
    Dec 2, 2003
  2. thomson
    Replies:
    10
    Views:
    2,513
    Eliyahu Goldin
    Jun 20, 2005
  3. thomson
    Replies:
    0
    Views:
    397
    thomson
    Jun 20, 2005
  4. Fred
    Replies:
    3
    Views:
    330
    Alf P. Steinbach
    Aug 10, 2003
  5. Alex Vinokur
    Replies:
    4
    Views:
    485
    Ron Natalie
    Sep 24, 2003
Loading...

Share This Page