in reply to character conversion in a loop...

Three ways to go character-by-character:

use split ('', $string) to return an array of characters.

use s/./g as the condtional of a while loop to match each character.

use substr for (1..length).

—John

Replies are listed 'Best First'.
Re: Re: character conversion in a loop...
by blakem (Monsignor) on Aug 06, 2001 at 00:03 UTC
    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

      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

Re: Re: character conversion in a loop...
by snafu (Chaplain) on Aug 05, 2001 at 22:47 UTC
    Hmm...so my way wouldn't have worked eh? So much for innovativeness.

    ----------
    - Jim

      Right, because “Remember: the value of $/ is a string, not a regex. awk has to be better for something. :-)”

      However, you could set it to an integer to read one character at a time.

      —John