in reply to Re: 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?

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";
  • Comment on Re^2: How can one create an array of the indices at which a given character appears in a string?
  • Select or Download Code

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

    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;
        > 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!