in reply to Re^2: Question on Regex grouping
in thread Question on Regex grouping

"Additional parens don't contribute that much".

Fair enough, there is overhead in doing it at all. I am saying "don't over do it".

list slice, hash slice, etc are some of the most cool features in Perl! You are completely correct in that list slice does not "play well with match global" because the number of things that can be returned is variable and therefore there is no way to specifiy a subset of range indicies that are of interest.

The classic example of list slice is used when spliting a line and you want 127,[3..5],93,8 things on that line. And I do work with DB lines like that - it is actually common for such a thing. List slice allows me to assign those 6 things directly into variables that mean something within the program. I usually assign vars on the left ($x,$y,$z..) in the order that the following code will use them. And adjust the slice accordingly.

If you are saying that "do not use list slice when doing a match global", I would absolutely agree with that. And I do not think that I have recommended that.

In your code, my $c = ("foo" =~ /($a)*$b/)[0]; is an improper use of list slice.

Properly used, list slice is beautiful.

Replies are listed 'Best First'.
Re^4: Question on Regex grouping
by JavaFan (Canon) on Dec 21, 2010 at 13:57 UTC
    In your code, my $c = ("foo" =~ /($a)*$b/)[0]; is an improper use of list slice.
    Then enlighten us, what is the "proper use" of list slices to avoid using $1, etc? Note that the OP used the match in an if statement, so whether the pattern matched or not is important.
    If you are saying that "do not use list slice when doing a match global", I would absolutely agree with that. And I do not think that I have recommended that.
    Well, you wrote:
    In general, I avoid using $1, $5 etc. Use Perl list slice instead.
    If you are using words like in general, followed by an unqualified demand what to use, be prepared for others pointing out cases where the "in general" doesn't work.
      In general, I avoid using $1, $5 etc. Use Perl list slice instead.

      I don't know how to make that more clearly stated. My code shows a very clear example of not using $1. My code also has the case that $string8 is undefined, which would happen if the match failed. Often in parsing, it is desired to keep going, not in this case perhaps but that does happen. I showed the place to do that if this is necessary - there is a comment block about that.

      In Perl 5.10, $string8 //= ''; Sets $string8 to null string if $string8 is undefined. In older Perl, we only had $string |= ''; which is not quite the same thing. The new Perl operator tests for "definedness" instead of "truthfulness". But anyway there is a place in the code to use that info.

      In your code: "Did it match, or didn't it? If $c is defined, it matched. But what if $c isn't? If $a eq "g", and $b eq "o", there is a match, but $c is undefined."

      You are saying that if $c is undefined, then the match didn't work. Ok. True. What else is there to say about this? I said that this was a misuse of list slice, because you were presenting this as case where list slice didn't work. Perhaps my English prose wasn't as quite as well written as it could have been. Ok, if $c is undefined, then there is no information other than "it didn't work". List slice will not "save the day" in this case. That is why I said it was a misuse.

      Javafan is a very, very high level Monk and you know perhaps even more than I do, that often what is asked in a post is not what is really needed. I offered a credible solution to what I thought the OP needed and in addition showed ways to extend that solution. The OP thanked me. So, what problems remain? I think none.

      You think that list slice is "ugly". Ok. It might very well be! I would suggest that we leave this thread and that you start a new thread: re: "what are proper uses of list slice?". And I am sure that this will be one of the most talked about threads in recent history.

        I don't know how to make that more clearly stated. My code shows a very clear example of not using $1
        You mean, this one:
        #$string8 = (m/def(\d{8})/)[0]; #alternate way with list slice
        How's that different from my:
        my $c = ("foo" =~ /($a)*$b/)[0];
        which you call an improper use of list slice.. Please spell it out for me, because I don't see the difference.
        My code also has the case that $string8 is undefined, which would happen if the match failed.
        Given the specific pattern, yes. But it doesn't work in general, as my example shows you can have patterns that match, but leave $1 undefined.

        So, let me repeat the question: How do I use list slices in a "proper way" to avoid using $1, while still getting an answer of the question "did the match succeed", and "if it matched, what was captured". In a general way please, not one that works for some patterns, and not for others.

        You are saying that if $c is undefined, then the match didn't work.
        No, I didn't. I said, if $c is undefined, then you do not know whether the match did, or did not, work.
        Ok, if $c is undefined, then there is no information other than "it didn't work". List slice will not "save the day" in this case.
        Bingo! That's what I've been saying.
        List slice will not "save the day" in this case. That is why I said it was a misuse.
        Oh, now I get it. You say, "In general, I avoid X. Use Y". But when I point out simple patterns where technique Y doesn't work, suddenly technique Y is a misuse of technique Y, even if I use it in exactly the same way as your example. In general, you'd be better of using X, cause that in general just always works. Not technique Y, which would actual inspection of the data before you can determine whether it works or not.

        Had you just said "I tend to avoid using $1, if possible I use a list slice", instead of wording it as "In general foo. Use blah.", I wouldn't have replied in the first place.