need help with the tr and s///

J

joez3

Hi,
Does anyone have an easy way to replace the end of one string with
another? I have 2 strings one that ends with some unknown number of 0,
and I need to replace the trailing chars in the other string with that
number of 0s. I figure I could do this with a few loops but there has
to be an easy way to use the s/// and the tr to help me out.
any ideas,
zim
 
T

Tad McClellan

Does anyone have an easy way to replace the end of one string with
another? I have 2 strings one that ends with some unknown number of 0,
and I need to replace the trailing chars in the other string with that
number of 0s.


---------------------------
#!/usr/bin/perl
use warnings;
use strict;

my $one_str = '1230000';
my $other_str = 'Laziness, Impatience and Hubris';

(my $zeros = $one_str) =~ s/.*?(?=0+$)//; # strip all but trailing zeros
substr($other_str, -length $zeros) = $zeros;

print "$other_str\n";

__END__
# a shorter, but much too ugly way:
substr($other_str, -length(($one_str =~ m/(0+)$/)[0]) ) =~ s/./0/g;
 
M

Mark Donovan

Does anyone have an easy way to replace the end of one string with
another? I have 2 strings one that ends with some unknown number of 0,
and I need to replace the trailing chars in the other string with that
number of 0s. I figure I could do this with a few loops but there has
to be an easy way to use the s/// and the tr to help me out.

Let me understand... you want the number of trailing zeros to be the same
for both strings. Here are two ways.

Find the trailing zeros on the first string, count them, remove trailing
zeros from the second string, and append new trailing zeros to the second
string.

$s1 =~ m/(0*)\z/;
$len = length $1;
$s2 =~ s/(.*?)0*\z/$1/;
$s2 .= '0' x $len;

Here's the same result with two statements that run about 15 percent faster.

($len = $s1) =~ s/.*?(0*)\z/length $1/e;
$s2 =~ s/(.*?)0*\z/$1 . '0' x $len/e;
 
M

Mark Donovan

You are right, of course, but the four line version was for illustration
purposes. I believe the individual steps make the more compact alternatives
easier to understand.
 
M

Mark Donovan

If the purpose of illustration isn't clear to you, I'm happy to explain.

As a matter of preference and for purposes of illustration, I carefully
chose the steps I used so that the person asking the question could insert
print statements, if needed, so that each step would be clear. The original
question indicated to me that zim is familiar with a C-like programming
language. To me, the extra steps are helpful to someone who may not be
familiar with the substitution features of perl. The extra steps allow
someone to examine the intermediate results. Moreover, my answer included a
method that avoided the extra steps. But for my purpose of illustrating the
method, your assertion is simply wrong.

As to your other remark, I find it irrelevant and refer you and others to
the posting guidelines for this newsgroup. Specifically, the "first
guidelines commandment" which I'll quote for you.
A note to newsgroup "regulars":
Do not use these guidelines as a "license to flame" or other
meanness. It is possible that a poster is unaware of things
discussed here. Give them the benefit of the doubt, and just
help them learn how to post, rather than assume that they do
know and are being the "bad kind" of Lazy.

You may have noticed that when I replied to the original question, I placed
my reply after the material I quoted. I did it for a good reason.

In a general sense, there *is* a good reason for posting replies after
quoted text. It is to help others who may read the message to follow the
revisions or sequence of changes that others have suggested. Beyond that,
however, placing a reply above or below quoted material is a matter of
personal preference. There is no program code that is relevant from your
reply, you simply failed to understand my purpose in showing individual
steps of the method I used.

You should understand that placing the word *please* in front of a sentence
does not make a rude and irrelevant demand into a polite request. You are
not enforcing rules. There's a line from the first "Pirates of the
Caribbean" movie, "the Code is more what you'd call 'guidelines' than actual
rules."

The posting guidelines for this newsgroup are guidelines, not rules. You
will find people, who for their own reasons, may not follow the guidelines
100 percent of the time. It's best to assume there may be a reason and avoid
making irrelevant remarks.

If you prefer more authoritative standards than the posting guidelines, I
refer you to:

"Netiquette Guidelines," http://rfc.net/rfc1855.html

http://en.wikipedia.org/wiki/Etiquette

http://en.wikipedia.org/wiki/Manners

http://www.campaignforcourtesy.org/
 
T

Tad McClellan

Mark Donovan said:
If the purpose of illustration isn't clear to you, I'm happy to explain.


What illustration?

As to your other remark,


What other remark?

I find it irrelevant and refer you and others to
the posting guidelines for this newsgroup. Specifically, the "first
guidelines commandment" which I'll quote for you.


I find your quote irrelevant, as there was no flaming or other meanness.

You may have noticed that when I replied to the original question, I placed
my reply after the material I quoted. I did it for a good reason.


And that reason has now stopped applying?

What changed?

In a general sense, there *is* a good reason for posting replies after
quoted text.


So why not just do that then?

What's all the hubbub about?

It is to help others who may read the message to follow the
revisions or sequence of changes that others have suggested.


Right. Like what illustration and what remark.

Beyond that,
however, placing a reply above or below quoted material is a matter of
personal preference.


I exercise my personal preference by killfiling top-posters.

There is no program code that is relevant from your
reply,


But there was other context that _was_ relevant in the previous replies.

You should understand that placing the word *please* in front of a sentence
does not make a rude and irrelevant demand into a polite request.


Requesting that you not top post is neither rude nor irrelevant.

The posting guidelines for this newsgroup are guidelines, not rules.


But there are (often unspoken) consequences should you choose to
not follow them.

You
will find people, who for their own reasons, may not follow the guidelines
100 percent of the time.


That is their choice.

It's best to assume there may be a reason and avoid
making irrelevant remarks.


I find it best to simply auto-ignore whiners.
 
A

Andrew DeFaria

Michele said:
I wrote "*please*" because it really bothers me to see top-posted
stuff, and it tends to make my life harder when replying further
because it increases the probability of requiring me to do editing
acrobatics, and there's a general consensus here that proper quoting
is preferred. Glad to notice you prefer to intentionally bother me,
and others here, for no particular good reason...
So, to review - you say "please" but deep down you don't really mean
that. You mean "DO IT!".

Bitch!

Plonk
 
M

Mark Donovan

Dondi,

It's unfortunate that your e-mail address doesn't work.

Delivery to the following recipient failed permanently:

(e-mail address removed)

Technical details of permanent failure:
PERM_FAILURE: SMTP Error (state 9): 552 RCPT TO:<[email protected]>
Mailbox disk quota exceeded

I take exception to your assertion. You are simply wrong. I, not you, decide
what steps are necessary to illustrate the method I suggest. If you think
you have a better solution, fine, post it. Your only contribution so far is
this obfuscated code which illustrates how badly perl can be abused.

$s2 =~ s/(.*?)0*\z/$1 . do{($s1 =~ m|(0*)\z|g)[0]}/e;

It makes nothing more clear. I don't like it. You said you don't like it.
You may have a solution. I haven't seen it.

You seem to believe I have used some unnecessary perl statements in my
answer. You said so. I agreed and I told you why. The perldocs have lots of
examples of code that is not optimal. Why won't you accept that I might want
to include steps that show something I think is important.

I don't need your instruction on how to answer questions. You have yet to
offer anything constructive or useful information to zim who asked the
original question.

The four statements I posted allow someone to insert print statement that
show the steps I chose to illustrate a method. For some reason, you felt
your method is more clear than mine. Fine! I agreed, my method is not
optimal, I said it. If you have a better method, post it. Instead, you
persist in trying to push your solution on me. But your comments don't
instruct me, I already know my code was not optimal. I explained that and
still you try to tell me how I must answer a question to suit your
preferences.

The reason I posted my reply over your quoted text is that I did not intend
for you to reply. You might have taken the hint. In any case, whether I post
above or below quoted material is not your concern. You certainly saw that
my original post was below the quoted material. So you can assume I
understand when to use that style and you can further assume that when I
don't do it, I have a reason.

Frankly, I don't care if you're annoyed by someone who posts above quoted
material in certain circumstances. It's not your place to correct me. You
entirely miss the point of posting guidelines. The guidelines are not rules
for you to enforce. As I explained, the purpose of posting after quoted
material is to make it easier for others to follow an extended discussion. I
responded and agreed with you that my illustration was not optimal perl
code. I let you know that my intent was to illustrate a method. No follow-up
is required.

You are free to post an your improved solution if you have one. If it works
and it's not obfuscated abuse of perl code, you can be assured I'll let your
solution stand without trying to improve it with irrelevant commentary.
 
A

anno4000

[...]
my $one_str = '1230000';

(my $zeros = $one_str) =~ s/.*?(?=0+$)//; # strip all but trailing zeros

Why not a simple capture?

my ( $zeros) = $one_str =~ /(0*)$/;

Anno
 
L

lucca70560

Michele said:
Well, I strongly prefer to keep the discussion public, but if you
really want to you can write me at

ti tod nfni tod im tod mcl ta razalb

(I hope you will decipher it, it's not that difficult - I won't post
it in any clearer form.)

I figured it out: (e-mail address removed)
 
L

lucca70560

Sherm said:
Yeah, that was the idea - any human with two working brain cells can easily
figure it out, but it's not sitting there in the clear for spambots to
harvest.

Anyone with an ounce of courtesy would have respected Michele's decision
to keep his address munged against automated spam harvesters. Reposting it
in the clear was the act of a petulant child. Grow up.

Anyone with an ounce of courtesy would have used a real email address,
so those of us that want to ask further questions in email could
actually reach her.
Web Hosting by West Virginians, for West Virginians: http://wv-www.net

West Virginia has computers???
 
M

Mark Donovan

Sherm,

The problem stems from Dondi's attempt to be cute with a "clever"
cryptogram. Dondi offered to continue a discussion with me by e-mail. The
thread has become a person-to-person matter that is no longer relevant to a
perl technical newsgroup. I was told, "I hope you will decipher it, it's not
that difficult - I won't post it in any clearer form."

In point of fact, it was *entirely* unnecessary for Dondi to to risk the
exposure of *his* e-mail address, since my address is posted in the clear.
 
L

lucca70560

Sherm said:
Michele is male - or at least I assume so, seeing as how most people who
wear a beard and moustache are of that gender, and "Michele" is an Italian
form of "Michael".

Michele *is* a girl's name, and plenty of Italian women have
mustaches...
Web Hosting by West Virginians, for West Virginians: http://wv-www.net

West Virginia has a computer???
 
A

Andrew DeFaria

Hey, va fa Napoli!
All this excellent, expert and highly technical Perl expertise flying
back and forth is amazing!

OK, children, it's time to stop your childishness.
 
G

Guest

Huh?!? Just because I want to avoid further spam on a particular email
address?


Michele

It's a troll--and probably a morph of Mark Donovan.

Don't waste your time.
 

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

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,252
Latest member
MeredithPl

Latest Threads

Top