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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Unnecessary use of capturing in regex?
by si_lence (Deacon) on Nov 03, 2004 at 09:03 UTC
    hello,
    Unless I'm completely blind the two bits of code are identical.
    So I would hope for the same results.

    The answer then would be:

    Because computers are supposed to be deterministic ;-)

    si_lence
Re: Unnecessary use of capturing in regex?
by erix (Prior) on Nov 03, 2004 at 09:04 UTC

    Why do you expect different output from each? The programs are exactly the same.

    Maybe you can rephrase your question?

Re: Unnecessary use of capturing in regex?
by theorbtwo (Prior) on Nov 03, 2004 at 09:04 UTC

    Um, they're exactly the same? Did you make an error copy-and-pasting?


    Warning: Unless otherwise stated, code is untested. Do not use without understanding. Code is posted in the hopes it is useful, but without warranty. All copyrights are relinquished into the public domain unless otherwise stated. I am not an angel. I am capable of error, and err on a fairly regular basis. If I made a mistake, please let me know (such as by replying to this node).

Re: Unnecessary use of capturing in regex?
by davido (Cardinal) on Nov 03, 2004 at 09:07 UTC

    I too am trying to figure this out. You've asked why the two identical scripts produce the same result. Consistancy is documented behavior. ;)


    Dave

Re: Unnecessary use of capturing in regex?
by Random_Walk (Prior) on Nov 03, 2004 at 09:08 UTC

    Well they look identical to me, did you cut and paste the same code twice or did Corion's janitoring (Added formatting) make two disimilar looking blocks become the same ?

    Cheers,
    R.

      No, Corion's edit did not alter the content of the message, he really did only touch formatting (code tags and such). There is an edit history available to Janitors, and we've reviewed it, determining that what you, the viewing audience are seeing is what the author wrote.


      Dave

Re: Unnecessary use of capturing in regex?
by theroninwins (Friar) on Nov 03, 2004 at 10:03 UTC
    I also thought that the 2 scripts are exactly the same but thought that I was missing somehting that's why i didn't write it but i guess I'm not the only one that sees it this way.
    So why the post???
    What is the problem??
    the answer to your given question is because they are the same

      I am so sorry that one of the codes just are the following:

      #!\usr\bin\perl -w for($_="head";s/(.)//;){ print"now head is $_.\n"; }
        ...tell me why the two can come to the same result...
        #!\usr\bin\perl -w for($_="head";s/(.)//;){ print"now head is $_.\n"; } #!\usr\bin\perl -w for($_="head";s/.//;){ print"now head is $_.\n"; }

        The only difference between your (updated) two samples, is that in the first you have capturing brackets. These will cause the matched character to be copied into $1. But then you do nothing with $1, so otherwise both samples are the same:

        Match a (the first) character in $_, and then delete it; print out the contents of $_; repeat until no match occurs, which will be when $_ is empty.

        What difference were you expecting?


        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "Think for yourself!" - Abigail
        "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon

        They give the same output because they are still technically the same.

        The only difference is the parenthesis in the regex around the "any single character" ( or "." ).

        The role of parenthesis here is to group parts of the regex together and to create back references ($1, $2 etc). See the perlre documentation for full details on what it does.

        In this particular case it's making no difference to the operation of the regex in question.

        --- Jay

        All code is untested unless otherwise stated.
        All opinions expressed are my own and are intended as guidance, not gospel; please treat what I say as such and as Abigail said Think for yourself.
        If in doubt ask.