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

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!

  • Comment on Re^3: How can one create an array of the indices at which a given character appears in a string? (updated)
  • Select or Download Code

Replies are listed 'Best First'.
Re^4: How can one create an array of the indices at which a given character appears in a string?
by Not_a_Number (Prior) on Jan 29, 2015 at 14:42 UTC
    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;
      > Capturing parens not needed

      Imho that's why hdb got (2,2)

      The first was the end for $& and the second for $1 and so on.

      Cheers Rolf

      PS: Je suis Charlie!

      Amazing!