perl - python equivalent
am 14.06.2005 21:46:16 von Rock
Hi, hope someone can clear up some confusion for me in converting some
python scripts to perl. My python scripts make frequent use of
replacing text from a string. For example, something like this below
that changes the word perl to python.
test = test.replace("http://python/python","http://perl/perl");
Is there a simple replace function like this in perl?
I tried below but it did not work. Is this even close, am I way off or
is there a better way?
$newlink = "s/\'/python/python'/'perl/perl'/g"
thx
Re: perl - python equivalent
am 14.06.2005 22:15:46 von Gunnar Hjalmarsson
Rock wrote:
> Hi, hope someone can clear up some confusion for me in converting some
> python scripts to perl. My python scripts make frequent use of
> replacing text from a string. For example, something like this below
> that changes the word perl to python.
>
> test = test.replace("http://python/python","http://perl/perl");
>
> Is there a simple replace function like this in perl?
>
> I tried below but it did not work. Is this even close, am I way off or
> is there a better way?
>
> $newlink = "s/\'/python/python'/'perl/perl'/g
That would be
s/\/python\/python/\/perl\/perl/g;
or more readable
s{/python/python}{/perl/perl}g;
Read about the s/// operator in "perldoc perlop".
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Re: perl - python equivalent
am 14.06.2005 22:45:29 von Rock
Gunnar Hjalmarsson wrote:
> Rock wrote:
>
>> Hi, hope someone can clear up some confusion for me in converting some
>> python scripts to perl. My python scripts make frequent use of
>> replacing text from a string. For example, something like this below
>> that changes the word perl to python.
>>
>> test = test.replace("http://python/python","http://perl/perl");
>>
>> Is there a simple replace function like this in perl?
>>
>> I tried below but it did not work. Is this even close, am I way off or
>> is there a better way?
>>
>> $newlink = "s/\'/python/python'/'perl/perl'/g
>
>
> That would be
>
> s/\/python\/python/\/perl\/perl/g;
>
> or more readable
>
> s{/python/python}{/perl/perl}g;
>
> Read about the s/// operator in "perldoc perlop".
>
Thanks! I have it working as above. I knew I was close but thought there
must be a better way in perl that is a little more clear. Particularly
if I have to go back and troubleshoot many different files. For example
I have lots of URLs that are changed in scripts that come in looking
something like this http://g.mssdfsdfn.com/sdfsdf/3?
python is simply a cut-in paste of the URL.
newlink = newslink.replace("http://g.mssdfsdfn.com/sdfsdf/3?","")
Where as perl
$newlink =~ s/http:\/\/g.mssdfsdfn.com\/sdfsdf\/3\?//g;
Re: perl - python equivalent
am 14.06.2005 23:45:08 von Jim Gibson
In article <1118778373.532154@nntp.acecape.com>, Rock wrote:
> Hi, hope someone can clear up some confusion for me in converting some
> python scripts to perl. My python scripts make frequent use of
> replacing text from a string. For example, something like this below
> that changes the word perl to python.
>
> test = test.replace("http://python/python","http://perl/perl");
>
> Is there a simple replace function like this in perl?
>
> I tried below but it did not work. Is this even close, am I way off or
> is there a better way?
>
> $newlink = "s/\'/python/python'/'perl/perl'/g"
You are close, but:
1. you need to use the binding operator '=~',
2. '-' is not a valid modifier for the s operator,
3. you don't need to enclose the search and replacement strings in
quotes, escaped or not, unless the quotes are part of your string, and
4. you do need to escape '/' if it is part of either string or else it
will be interpreted as the end of the string.
So use:
$newlink =~ s/python\/python/perl\/perl/g;
but you can use a different delimiter to avoid escaping the slashes:
$newlink =~ s|python/python|perl/perl|g;
----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= East/West-Coast Server Farms - Total Privacy via Encryption =---
Re: perl - python equivalent
am 14.06.2005 23:51:29 von Gunnar Hjalmarsson
Rock wrote:
> Gunnar Hjalmarsson wrote:
>> Rock wrote:
>>> My python scripts make frequent use of
>>> replacing text from a string. For example, something like this below
>>> that changes the word perl to python.
>>>
>>> test = test.replace("http://python/python","http://perl/perl");
>>>
>>> Is there a simple replace function like this in perl?
>>
>> s/\/python\/python/\/perl\/perl/g;
>>
>> or more readable
>>
>> s{/python/python}{/perl/perl}g;
>
> Thanks! I have it working as above. I knew I was close but thought there
> must be a better way in perl that is a little more clear. Particularly
> if I have to go back and troubleshoot many different files. For example
> I have lots of URLs that are changed in scripts that come in looking
> something like this http://g.mssdfsdfn.com/sdfsdf/3?
>
> python is simply a cut-in paste of the URL.
>
> newlink = newslink.replace("http://g.mssdfsdfn.com/sdfsdf/3?","")
>
> Where as perl
>
> $newlink =~ s/http:\/\/g.mssdfsdfn.com\/sdfsdf\/3\?//g;
Quote the pattern metacharacters using \Q and either use some other
delimiter than '/':
$newlink =~ s#\Qhttp://g.mssdfsdfn.com/sdfsdf/3?##g;
or store the URL in a variable:
my $oldurl = 'http://g.mssdfsdfn.com/sdfsdf/3?';
$newlink =~ s/\Q$oldurl//g;
How are not those options not clear enough?
If efficiency is an issue, you may want to consider a combination of the
substr() and index() functions instead, but that would not make the code
easier to read.
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Re: perl - python equivalent
am 15.06.2005 01:01:09 von Rock
Gunnar Hjalmarsson wrote:
> Quote the pattern metacharacters using \Q and either use some other
> delimiter than '/':
>
> $newlink =~ s#\Qhttp://g.mssdfsdfn.com/sdfsdf/3?##g;
>
> or store the URL in a variable:
>
> my $oldurl = 'http://g.mssdfsdfn.com/sdfsdf/3?';
> $newlink =~ s/\Q$oldurl//g;
>
> How are not those options not clear enough?
>
The first example above is what I was looking for. Now I can just cut-in
paste the url into each script between $newlink =~ s#\Q and ##g;
Thx man!