perlbeginner10 has asked for the wisdom of the Perl Monks concerning the following question:

Hi guys,

I have a small problem, I guess it should be hard. I have an array of words.

@array = ('dave', 'brian','alex's','eddie's');
I want to scan through the array and if " ' " is found, replace with " \' ".
Thanks a lot.

Update:

Hi,

Got the answer already!!

Replies are listed 'Best First'.
Re: finding and replacing
by McDarren (Abbot) on Dec 04, 2005 at 17:25 UTC
    Well, first of all - your array as you have it produces an error:
    #!/usr/bin/perl -w use strict; my @array = ('dave', 'brian','alex's','eddie's'); print "@array\n";

    If we try to execute that, we get:

    String found where operator expected at array.pl line 4, at end of lin +e (Missing semicolon on previous line?) syntax error at array.pl line 4, near "'alex's','eddie's" Can't find string terminator "'" anywhere before EOF at array.pl line +4.

    A better way to define your array is like so:

    my @array = qw(dave brian alex's eddie's);

    Which gives us the correct output:

    barney:/home/darren/perlmonks# perl array.pl dave brian alex's eddie's

    An explaination of the use of qw can be found under the "Regexp Quote-Like Operators" section in perlop

    Hope this helps,
    Darren :)

Re: finding and replacing
by NetWallah (Canon) on Dec 04, 2005 at 17:21 UTC
    my @a = qw(dave brian alex's eddie's); s/'/\\'/ for @a; print qq($a[3]\n); --Output -- eddie\'s

         You're just jealous cause the voices are only talking to me.

         No trees were killed in the sending of this message.    However, a large number of electrons were terribly inconvenienced.

      Hi, In my case, this works for only the first " ' ", not all. If there are two or more " ' ", the program doesn't work. Can anyone explain why?
        You need to post your code for us to be able to help you.

        Simply saying it does not do what you expect does not help - we need to know what "it" is before we can help.

        Update: - I'm guessing what you mean is that if a particular array element contains more than one single-quote, only the first single-quote gets escaped/replaced, in the code I posted.

        This is correct. If you need all of them replaced, use the "g" option in the regular expression .

        s/'/\\'/g for @a;

             You're just jealous cause the voices are only talking to me.

             No trees were killed in the sending of this message.    However, a large number of electrons were terribly inconvenienced.

Re: finding and replacing
by serf (Chaplain) on Dec 04, 2005 at 23:54 UTC
    Could you possibly put your question back please?

    This node (unless it gets reaped) will be around for a long time, complete with the answers that the other monks have given up their own time to write for you, which hopefully could provide useful information to help anyone else who came along in the future with a similar question... except that you've deleted your question, so their answers are dangling meaningless in cybervoid... :o(

    Update:

    for context: The question in this node had been removed and replaced with "Got the answer already!!"

    Thank you for restoring the question :o)

      It works for the greater good of the monastry to have original authors understand the value of their own nodes.

      Especially bearing in mind that everyone's posts collectively (both questions and answers) help make this place a valuable repository of information on the Net (and of course individually are an important contributor to one's XP!)

      Therefore, in the spirit of 459456 it should be better to encourage people to maintain their own contributions rather than having to get janitors come and clean up after us all, because that way authors know how to do it right next time and the janitors have more time to do their excellent work elsewhere!