in reply to Substring consisting of all the characters until "character X"?

Wow. You're using Perl and you're still not aware of one of the traditional selling points of Perl: regular expressions.
($substring) = $fullstring =~ /^.{2}(.*?)X/s;
That's everything from behind the second character up till the first "X".
  • Comment on Re: Substring consisting of all the characters until "character X"?
  • Download Code

Replies are listed 'Best First'.
Re^2: Substring consisting of all the characters until "character X"?
by davies (Monsignor) on Mar 14, 2011 at 12:20 UTC

    I've been looking for something like this. I even understand most of it. But could you (or someone) please explain why ($substring) needs to be in brackets? I've looked in perlre, perlrequick and perlretut and can't see this construct, although it's always possible that it's there & I've missed it.

    Regards,

    John Davies

      That has nothing to do with regexes (and that's why you didn't find it in perlre) but everything with context of the assignment.

      Perl has 2 main contexts: scalar context, and list context. A function may behave differently depending on the context. (Actually there are 3 contexts: void context is the third, but it's often treated as a special case of scalar context.) For example: and array returns the array items in list context, and the number of items in scalar context. Example:

      @array = ('a', 'b', 'c'); $x = @array; # scalar context => 3 @y = @array; # list context => ('a', 'b', 'c') ($v, $w) = @array; # list context so $v => 'a', $w => 'b'

      When you put parentheses around the assignees on the left of the assignment, you get list context. The result is flattened to a list (individual items) and the items on the left get assigned the value of the item at their own position in the list. If there are too few items, the rest gets assigned undef; if there are too many, the remainder is ignored.

      And that is what's happening here: the regex is called in list context so it returns the captured items (the value for the patterns in parens) and from that list the value of $1 is assigned to $substring.

      Official docs: "Context" in perldata — see also wantarray for making your own functions behave differently depending on context; and scalar to force scalar context on a function call.

      For regexes, the docs on context are in perlop (because the // is considered a kind of quotes, and quotes are treated as operators.)

      The parentheses provide list context, in which a pattern match returns the captured parts.  In scalar context, the return value is just true/false depending on whether the regex matched.

      See perlop.

        I didn't understand the documentation you pointed me to, but I understood your explanation perfectly! Thanks.

        Regards,

        John Davies