in reply to Re: character conversion in a loop...
in thread character conversion in a loop...

I think you munged your second suggestion (which is the one I typically use) I believe you meant.

/(.)/g

As in:

while($string =~ /(.)/g) { my $letter = $1; .... }

-Blake

Replies are listed 'Best First'.
Re: Re: Re: character conversion in a loop...
by John M. Dlugosz (Monsignor) on Aug 06, 2001 at 01:02 UTC
    It works: Without the parens, you can use $&, rather than $1 as you showed.

    —John

      ewww.. isn't $& one of those variables that you should almost never use (along with $' and $`) because it slows down every pattern match afer that?

      Actually I was refering to the s in your suggestion: s/./g wont even compile.

      m/./g or equivalently /./g will work with $& as you suggested. s/.// will also work (note the lack of the g modifier), though it destroyes the original string.

      I'm guessing you meant /./g which is why I think it was a typo.

      -Blake

        The problem with "never use" had to do with some mode that would cause the string to be copied, and if it was used anywhere in the program it would stay on. But, that's been addressed with newer versions, and you copy it by using $1 anyway, so it's no worse. Try benchmarking it to see if saving the capture but capturing the whole match comes out ahead or behind or no change. I kind of figured that saving the capture was going to come out ahead.

        Yes, I meant s, not m.

        —John