newbie Perl question

Discussion in 'Perl Misc' started by Westcoast Sheri, May 29, 2005.

  1. 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.
     
    Westcoast Sheri, May 29, 2005
    #1
    1. Advertising

  2. 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/"$/."/;
     
    RedGrittyBrick, May 29, 2005
    #2
    1. Advertising

  3. 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.
     
    Brian McCauley, May 29, 2005
    #3
  4. > 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.
     
    Westcoast Sheri, May 29, 2005
    #4
  5. 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
     
    Gunnar Hjalmarsson, May 29, 2005
    #5
  6. Something tells me it took you a very long time to post that nugget of
    wisdom....

    > From: Gunnar Hjalmarsson <>
    > 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
     
    Westcoast Sheri, May 29, 2005
    #6
  7. 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!



    It is no longer _my_ problem.

    *plonk*


    > Good day.



    Yeah, right.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, May 30, 2005
    #7
  8. Would you repeat that, please?

    > It is no longer _my_ problem.
    >
    > *plonk*
     
    Westcoast Sheri, May 30, 2005
    #8
  9. Westcoast Sheri <>
    wrote in
    news:BEBF89D8.748D7%
    :

    >> 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 <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, May 30, 2005
    #9
  10. [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
     
    Jürgen Exner, May 30, 2005
    #10
  11. Westcoast Sheri

    Anno Siegel Guest

    Westcoast Sheri <> wrote in comp.lang.perl.misc:
    > > 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!


    Oh... a smartass. So long.

    Anno
     
    Anno Siegel, May 30, 2005
    #11
  12. Jürgen Exner wrote:

    > [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***


    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.
     
    Brian McCauley, May 30, 2005
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dpackwood
    Replies:
    3
    Views:
    1,824
  2. Jerry C.
    Replies:
    8
    Views:
    253
    Uri Guttman
    Nov 23, 2003
  3. PerlFAQ Server

    FAQ 1.4 What are Perl 4, Perl 5, or Perl 6?

    PerlFAQ Server, Jan 23, 2011, in forum: Perl Misc
    Replies:
    0
    Views:
    310
    PerlFAQ Server
    Jan 23, 2011
  4. PerlFAQ Server
    Replies:
    0
    Views:
    699
    PerlFAQ Server
    Feb 3, 2011
  5. PerlFAQ Server

    FAQ 1.4 What are Perl 4, Perl 5, or Perl 6?

    PerlFAQ Server, Feb 27, 2011, in forum: Perl Misc
    Replies:
    0
    Views:
    302
    PerlFAQ Server
    Feb 27, 2011
Loading...

Share This Page