newbie Perl question

W

Westcoast Sheri

Perl is looping through thousands of sentences. Each sentence begins and
ends with a quotation mark (")

In my script, these sentences are represented by "$data[2]"

What is the "perl command" to replace the LAST occurrence of each quotation
mark, with a period and a quotation mark?

Example

"The day has come"
"He is happy"

....here's what I want:
"The day has come."
"He is happy."

I tried this:

$old = '\"';
$new = '\.\"';
sub replace_word {
local($str, $old, $new) = @_;
substr($str,rindex($str,$old),length($old)) = $new;
$str;
}
replace_word($data[2]);

....but that doesn't work.
Thank you for any help.
 
R

RedGrittyBrick

Westcoast said:
Perl is looping through thousands of sentences. Each sentence begins and
ends with a quotation mark (")

In my script, these sentences are represented by "$data[2]"

What is the "perl command" to replace the LAST occurrence of each quotation
mark, with a period and a quotation mark?

Example

"The day has come"
"He is happy"

...here's what I want:
"The day has come."
"He is happy."

I tried this:

$old = '\"';
$new = '\.\"';
sub replace_word {
local($str, $old, $new) = @_;
substr($str,rindex($str,$old),length($old)) = $new;
$str;
}
replace_word($data[2]);

...but that doesn't work.
Thank you for any help.

$data[2] =~ s/"$/."/;
 
B

Brian McCauley

Westcoast said:
Subject: newbie Perl question

Please put the subject of your post in the Subject of your post.

Be aware that the subject you have used will be read by mant people as
"Question I'm too lazy to figure out of myself. I'm too lazy to even
bother working out how what my question actually is".
Perl is looping through thousands of sentences. Each sentence begins and
ends with a quotation mark (")

In my script, these sentences are represented by "$data[2]"

What is the "perl command" to replace the LAST occurrence of each quotation
mark, with a period and a quotation mark?

What do you mean by "the LAST occurrence of each quotation mark"? Each
quotation mark can onlt appear once.
Example

"The day has come"
"He is happy"

...here's what I want:
"The day has come."
"He is happy."

Do you mean repace a quotation mark if it is the last character in the
string?

s/"$/."/;

Or do you mean replace the last quotation mark in the string?

s/"(?=[^"]*$)/."/;

or

s/(.*)"/$1."/;
I tried this:

$old = '\"';
$new = '\.\"';

Perhaps you should print out those strings and look what's in them.
You'll find there really are literal backslashes in there.
sub replace_word {
local($str, $old, $new) = @_;

In Perl one does not use local() to to introduce variables that are
local to a subrutine or block. One uses my(). For historical reasons
local() does something rather different (for details RTFM). Note this
will not actually prevent your code from working but unless you
understand what local() does you should not use it.

If you have a book or tutorial that shows local() being used in this way
it is hopelessly out of date (my() came in at 5.0 IIRC) and you should
discard it.
substr($str,rindex($str,$old),length($old)) = $new;

Perl has powerful regular expression based string manipulation tools.
Solutions based on substr/index/rindex can sometimes be a bit faster but
they are rarely as simple.

That said your solution should work if $old and $new contained the
correct values.
$str;
}
replace_word($data[2]);

You defined replace_word() as taking 3 arguments but only pass it one.
Within replace_word the variables $old and $new will therefore be undefined.

You defined replace_word() returning a modified copy of the string
(rather than changing the string passed to it in situ) but you don't do
anything with the return value.
Thank you for any help.

Please see the posting guidelines. They contain much useful advice on
how you can help yourself and help others to help you.
 
W

Westcoast Sheri

Please put the subject of your post in the Subject of your post.
Be aware that the subject you have used will be read by mant people as
"Question I'm too lazy to figure out of myself. I'm too lazy to even
bother working out how what my question actually is".

That is their (your) problem!
What do you mean by "the LAST occurrence of each quotation mark"? Each
quotation mark can onlt appear once.

"Onlt appear once," he says......

Shall we work on your SPELLING, or shall we work on those, "definitions."
Let's tackle definitions, first. I'll try to help you with that real
pickle-of-a-word, "last." Here, let me help. I'll use it in an example: "You
are the LAST person that should be answering this post, if you don't
understand the word, 'last.'"

Do you mean repace a quotation mark if it is the last character in the
string?

s/"$/."/;

Or do you mean replace the last quotation mark in the string?

s/"(?=[^"]*$)/."/;

or

s/(.*)"/$1."/;

Why are you even bothering to "show off," with all kinds of,
"do-you-mean-this" examples? The previous poster answered my question
elegantly, in one simple line. Kudos to him, and a heartfelt thanks to him
for not asking me to explain that I spent hours, already, trying to find an
answer to my question.

The rest of your post merely takes up bandwidth. Particularly because my
question was already answered.

Perhaps YOU should read the guidelines.

Good day.
 
G

Gunnar Hjalmarsson

Westcoast said:
That is their (your) problem!

Now it's also your problem, because the most knowledgable people here
won't likely concern themselves with another question from you.
 
W

Westcoast Sheri

Something tells me it took you a very long time to post that nugget of
wisdom....
 
A

A. Sinan Unur

Westcoast Sheri <[email protected]>
wrote in
(e-mail address removed):
That is their (your) problem!

It is your problem because, after seeing your response, people will be
unwilling to help you the next time you have a question.
What do you mean by "the LAST occurrence of each quotation mark"?
Each quotation mark can onlt appear once.
....
Do you mean repace a quotation mark if it is the last character in
the string?

s/"$/."/;

Or do you mean replace the last quotation mark in the string?

s/"(?=[^"]*$)/."/;

or

s/(.*)"/$1."/;

Why are you even bothering to "show off," with all kinds of,

That is not showing off. Brian was trying to help you learn how to frame
a question to make it easier for you to get the appropriate answers.
"do-you-mean-this" examples? The previous poster answered my question

Who is the previous poster?
The rest of your post merely takes up bandwidth. Particularly because
my question was already answered.

Perhaps YOU should read the guidelines.

*PLONK*
 
B

Brian McCauley

Jürgen Exner said:
[Please to not top-post, trying to repair]

Westcoast said:
Would you repeat that, please?


Dear Westcoast

Your wish is my command:

***PLONK***

Oh, the irony. The OP lashes out at me for trying to help and Tad,
Anno, Sinan and Jue all plonk[1] him!

And, of course, although I don't plonk I'll obviously be disinclined to
be helpful towards the OP. Gunnar also chimed in so that quite possibly
goes for him too.

I wonder between the six of us what proportion of the quality answers we
provide? About half I'd hazzard.

[1] For those new to Usenet, plonking means to publically announce that
one is configuring ones newsreader to ingore subsequent posts from a
given poster.
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top