Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > newbie Perl question

Reply
Thread Tools

newbie Perl question

 
 
Westcoast Sheri
Guest
Posts: n/a
 
      05-29-2005
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.

 
Reply With Quote
 
 
 
 
RedGrittyBrick
Guest
Posts: n/a
 
      05-29-2005
Westcoast Sheri wrote:
> 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/"$/."/;

 
Reply With Quote
 
 
 
 
Brian McCauley
Guest
Posts: n/a
 
      05-29-2005
Westcoast Sheri wrote:

> 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.
 
Reply With Quote
 
Westcoast Sheri
Guest
Posts: n/a
 
      05-29-2005
> 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.

 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      05-29-2005
Westcoast Sheri wrote:
>> 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!


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

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
Westcoast Sheri
Guest
Posts: n/a
 
      05-29-2005
Something tells me it took you a very long time to post that nugget of
wisdom....

> From: Gunnar Hjalmarsson <(E-Mail Removed)>
> Newsgroups: comp.lang.perl.misc
> Date: Mon, 30 May 2005 00:29:47 +0200
> Subject: Re: newbie Perl question
>
> Westcoast Sheri wrote:
>>> 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!

>
> Now it's also your problem, because the most knowledgable people here
> won't likely concern themselves with another question from you.
>
> --
> Gunnar Hjalmarsson
> Email: http://www.gunnar.cc/cgi-bin/contact.pl


 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      05-30-2005
Westcoast Sheri <westshestcostsheastsheri@westshestcostsheastsher. com> wrote:
>> 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!



It is no longer _my_ problem.

*plonk*


> Good day.



Yeah, right.


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Westcoast Sheri
Guest
Posts: n/a
 
      05-30-2005
Would you repeat that, please?

> It is no longer _my_ problem.
>
> *plonk*


 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      05-30-2005
Westcoast Sheri <westshestcostsheastsheri@westshestcostsheastsher. com>
wrote in
news:BEBF89D8.748D7%
(E-Mail Removed) om:

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


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*

--
A. Sinan Unur <(E-Mail Removed)>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      05-30-2005
[Please to not top-post, trying to repair]

Westcoast Sheri wrote:
>> It is no longer _my_ problem.
>>
>> *plonk*

>
> Would you repeat that, please?


Dear Westcoast

Your wish is my command:

***PLONK***

jue


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 2.17 What is perl.com? Perl Mongers? pm.org? perl.org? cpan.org? PerlFAQ Server Perl Misc 0 04-04-2011 10:00 PM
FAQ 1.4 What are Perl 4, Perl 5, or Perl 6? PerlFAQ Server Perl Misc 0 02-27-2011 11:00 PM
FAQ 2.17 What is perl.com? Perl Mongers? pm.org? perl.org? cpan.org? PerlFAQ Server Perl Misc 0 02-03-2011 11:00 AM
FAQ 1.4 What are Perl 4, Perl 5, or Perl 6? PerlFAQ Server Perl Misc 0 01-23-2011 05:00 AM
Perl Help - Windows Perl script accessing a Unix perl Script dpackwood Perl 3 09-30-2003 02:56 AM



Advertisments