Home > Archive > PERL Beginners > February 2005 > simple substitution question
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
simple substitution question
|
|
| Harold Castro 2005-02-18, 3:56 am |
| Hi,
There is something that's bothering me for so long
regarding the use of $_ variable.
for example:
here is my string:
$_ = "but";
s/u/a/g;
print $_;
This will simply print "bat"
My problem is using a variable in place of $_.
how will I tell that the one i'm going to substitute
is the $string;
Ex:
my $string = "but";
s/u/a/g;
print $string;
and I get this error when i run it.
Use of uninitialized value in substitution (s///) at
substitute line 6.
I don't have any trouble with regex matching using $_
or any varible because I can always say:
if $variable =~/\d/ instead of:
if(/\d/) which is equivalent to $_=~/\d/;
I don't know how to do it with substitutions.
any idea?
thanks
__________________________________
Do you Yahoo!?
Read only the mail you want - Yahoo! Mail SpamGuard.
http://promotions.yahoo.com/new_mail
| |
| Bedanta Bordoloi 2005-02-18, 8:56 am |
|
Hi,
If I've understood your problem, what you can simply try is
$string = "but";
print "Before: $string ";
$string =~ s/u/a/g;
print "After: $string\n";
You will get as output
$ Before: but After: bat
Cheers,
Bedanta
-----Original Message-----
From: Harold Castro [mailto:b0ydaem0n@yahoo.com]
Sent: Friday, February 18, 2005 12:17 PM
To: beginners@perl.org
Subject: simple substitution question
Hi,
There is something that's bothering me for so long
regarding the use of $_ variable.
for example:
here is my string:
$_ = "but";
s/u/a/g;
print $_;
This will simply print "bat"
My problem is using a variable in place of $_.
how will I tell that the one i'm going to substitute
is the $string;
Ex:
my $string = "but";
s/u/a/g;
print $string;
and I get this error when i run it.
Use of uninitialized value in substitution (s///) at
substitute line 6.
I don't have any trouble with regex matching using $_
or any varible because I can always say:
if $variable =~/\d/ instead of:
if(/\d/) which is equivalent to $_=~/\d/;
I don't know how to do it with substitutions.
any idea?
thanks
__________________________________
Do you Yahoo!?
Read only the mail you want - Yahoo! Mail SpamGuard.
http://promotions.yahoo.com/new_mail
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
| |
| Ing. Branislav Gerzo 2005-02-18, 8:56 am |
| Harold Castro [HC], on Thursday, February 17, 2005 at 22:47 (-0800
(PST)) typed:
HC> for example:
HC> here is my string:
HC> $_ = "but";
HC> s/u/a/g;
HC> print $_;
what about this:
( my $string = "but" ) =~ s/u/a/g;
--
...m8s, cu l8r, Brano.
[Old Farts don't have to be politically correct.]
| |
| Manav Mathur 2005-02-18, 8:56 am |
|
=~ is the bind operator.
see perldoc perlop.
Manav
-----Original Message-----
From: Harold Castro [mailto:b0ydaem0n@yahoo.com]
Sent: Friday, February 18, 2005 12:17 PM
To: beginners@perl.org
Subject: simple substitution question
Hi,
There is something that's bothering me for so long
regarding the use of $_ variable.
for example:
here is my string:
$_ = "but";
s/u/a/g;
print $_;
This will simply print "bat"
My problem is using a variable in place of $_.
how will I tell that the one i'm going to substitute
is the $string;
Ex:
my $string = "but";
s/u/a/g;
print $string;
and I get this error when i run it.
Use of uninitialized value in substitution (s///) at
substitute line 6.
I don't have any trouble with regex matching using $_
or any varible because I can always say:
if $variable =~/\d/ instead of:
if(/\d/) which is equivalent to $_=~/\d/;
I don't know how to do it with substitutions.
any idea?
thanks
__________________________________
Do you Yahoo!?
Read only the mail you want - Yahoo! Mail SpamGuard.
http://promotions.yahoo.com/new_mail
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
****************************************
*****************
Disclaimer:
The contents of this E-mail (including the contents of the enclosure(s) or attachment(s) if any) are privileged and confidential material of MBT and should not be disclosed to, used by or copied in any manner by anyone other than the intended addressee(s)
. In case you are not the desired addressee, you should delete this message and/or re-direct it to the sender. The views expressed in this E-mail message (including the enclosure(s) or attachment(s) if any) are those of the individual sender, except wh
ere the sender expressly, and with authority, states them to be the views of MBT.
This e-mail message including attachment/(s), if any, is believed to be free of any virus. However, it is the responsibility of the recipient to ensure that it is virus free and MBT is not responsible for any loss or damage arising in any way from its us
e
****************************************
*****************
|
|
|
|
|