in reply to How can one create an array of the indices at which a given character appears in a string?

the @- and @+ dynamic arrays supply the indices of successful matches of X in m/(X)/g

Cheers Rolf

PS: Je suis Charlie!

  • Comment on Re: How can one create an array of the indices at which a given character appears in a string?
  • Download Code

Replies are listed 'Best First'.
Re^2: How can one create an array of the indices at which a given character appears in a string?
by hdb (Monsignor) on Jan 29, 2015 at 14:28 UTC

    Exactly, how does this work? The following code prints 2 2 and not 1 6.

    use warnings; use strict; my $input = 'rnbqkbnr'; $input =~ /(n)/g; print "@+\n";

      Maybe I misunderstood perlvar and it doesn't work with /g in list context but only with repeated match groups in a non-global match.

      Anyway you should take @- for start, your results show the ends.

      Can't test ATM...sorry.

      update
      Without being able to test, please try

      print $-[0] while /n/g

      update

      now tested:

      DB<100> $_ = " x " x 3 => " x x x " DB<101> print $-[0] while /x/g 147

      Cheers Rolf

      PS: Je suis Charlie!

        my $input = 'rnbqkbnr'; my @results; push @results, $-[0] while $input =~ /(n)/g; print "@results";

        Update: Capturing parens not needed:

        push @results, $-[0] while $input =~ /n/g;
Re^2: How can one create an array of the indices at which a given character appears in a string?
by tkguifan (Scribe) on Jan 29, 2015 at 14:04 UTC
    What is the exact code here? The other solutions work for me ( I even understand how they work ), but I can't make this one work.