substituting $ to \$ in a var

D

dmedhora

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!
 
G

Gunnar Hjalmarsson

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;
 
T

Tad McClellan

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?
 
G

Gunnar Hjalmarsson

Tad said:
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?
 
A

A. Sinan Unur

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
 
T

Tad McClellan

Gunnar Hjalmarsson said:
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?

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.
 
R

robic0

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.




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 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!
 
G

Gunnar Hjalmarsson

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

True, but...
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.
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. ;-)
 
R

robic0

True, but...


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


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
 
T

Tad McClellan

Gunnar Hjalmarsson said:
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.



I disagree with your opinions frequently.
 
D

dmedhora

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top