For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > May 2005 > newbie Perl 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 newbie Perl question
Westcoast Sheri

2005-05-29, 3:56 pm

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($ol
d)) = $new;
$str;
}
replace_word($data[2]);

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

RedGrittyBrick

2005-05-29, 3:56 pm

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($ol
d)) = $new;
> $str;
> }
> replace_word($data[2]);
>
> ...but that doesn't work.
> Thank you for any help.
>


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

Brian McCauley

2005-05-29, 3:56 pm

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($ol
d)) = $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.
Westcoast Sheri

2005-05-29, 8:56 pm

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

Gunnar Hjalmarsson

2005-05-29, 8:56 pm

Westcoast Sheri wrote:
>
> 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
Westcoast Sheri

2005-05-29, 8:56 pm

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

> From: Gunnar Hjalmarsson <noreply@gunnar.cc>
> Newsgroups: comp.lang.perl.misc
> Date: Mon, 30 May 2005 00:29:47 +0200
> Subject: Re: newbie Perl question
>
> Westcoast Sheri wrote:
>
> 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


Tad McClellan

2005-05-30, 3:57 am

Westcoast Sheri < westshestcostsheastsheri@westshestcostsh
eastsher.com> wrote:
>
> That is their (your) problem!



It is no longer _my_ problem.

*plonk*


> Good day.



Yeah, right.


--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
Westcoast Sheri

2005-05-30, 3:57 am

Would you repeat that, please?

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


A. Sinan Unur

2005-05-30, 3:57 am

Westcoast Sheri < westshestcostsheastsheri@westshestcostsh
eastsher.com>
wrote in
news:BEBF89D8.748D7%
westshestcostsheastsheri@westshestcostsh
eastsher.com:

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


....
[color=darkred]
>
> 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 <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/c...guidelines.html
Jürgen Exner

2005-05-30, 8:57 am

[Please to not top-post, trying to repair]

Westcoast Sheri wrote:
>
> Would you repeat that, please?


Dear Westcoast

Your wish is my command:

***PLONK***

jue


Anno Siegel

2005-05-30, 8:57 am

Westcoast Sheri < westshestcostsheastsheri@westshestcostsh
eastsher.com> wrote in comp.lang.perl.misc:
>
> That is their (your) problem!


Oh... a smartass. So long.

Anno
Brian McCauley

2005-05-30, 8:57 am



Jürgen Exner wrote:

> [Please to not top-post, trying to repair]
>
> Westcoast Sheri wrote:
>
>
>
> 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.
Henry Law

2005-05-30, 8:57 pm

On Sun, 29 May 2005 15:38:39 -0700, Westcoast Sheri
< westshestcostsheastsheri@westshestcostsh
eastsher.com> wrote:

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


Ms Westcoast, let me explain how it is in this group. Some of the
best Perl people in the world hang out here; they can give really good
advice - not only on Perl but also on how to help yourself frame
questions and find answers.

You may not like the things that some of them say, but as they say in
English English "like it or lump it". You may have got some annoyance
off your chest, but you've done it at your own expense: you have
ensured that the real mavens don't ever see your posts, and of course
they won't help with your questions. I don't think that was too
clever. Now you have to hope that the rest of us who haven't (yet)
put you in the kill file can help.
--

Henry Law <>< Manchester, England
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com