http://www.perlmonks.org?node_id=215011


in reply to Re: Forcing array context
in thread Forcing array context

A reasonable solution, but I want to do this without
invoking the regex engine, so split is strictly taboo...Any other ideas?
--
Microsoft delendum est.

Replies are listed 'Best First'.
Re: Re: Re: Forcing array context
by jarich (Curate) on Nov 22, 2002 at 05:47 UTC
    If substr's okay, how about this kind of thing:
    my $string = "This is a very long string"; foreach (0..length($string)) { if(substr($string, $_, 1) eq " ") { # Replace this position (space) with a "|" substr($string, $_, 1, "|"); } } print $string;
    It kinda depends on what you're trying to do, but I think this might help. (Note that you can also replace with the empty string).

    Hope it does.

    jarich

    Update: forgot the length. ;)

•Re: Re: Re: Forcing array context
by merlyn (Sage) on Nov 22, 2002 at 13:00 UTC
    A reasonable solution, but I want to do this without invoking the regex engine
    When you say, "I wanna do $X", and the answer is "by doing $Y", and then you say "I wanna do $X but without doing $Y", you are obligated to provide a reason why the best/easiest way ($Y) is so easily dismissed. How close to doing $Y can you get, for example, and why not $Y? {grin}

    You haven't yet. Please do so.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

Re: Re: Re: Forcing array context
by helgi (Hermit) on Nov 22, 2002 at 14:23 UTC
    What's the deal with imposing ridiculous, artificial restrictions? Split is the correct way to do it, so there.

    --
    Regards,
    Helgi Briem
    helgi AT decode DOT is