mt2k has asked for the wisdom of the Perl Monks concerning the following question:

Say I have the following string:

$str = "hello=this=is=a=nice=sentence=with=many=equation=signs";

I wish to split this string up into an array on every second occurence of the '=' sign. Therefore, the structure I wish for is this:

@splitted = ( 'hello=this', 'is=a', 'nice=sentence', 'with=many', 'equ +ation=signs' );

My problem is I wonder whether it is possible to do so using the split() command or whether a regex is required to accomplish this task.

I have searched the site and come up with one topic, though I am not sure whether this is the same idea and how I would apply it.

Thanks for the help!

Replies are listed 'Best First'.
(jeffa) Re: Splitting on Every Second Occurence
by jeffa (Bishop) on May 04, 2002 at 19:59 UTC
Re: Splitting on Every Second Occurence
by Kanji (Parson) on May 04, 2002 at 20:09 UTC

    A regex would be simplest...

    push @splitted, $1 while $str =~ /([^=]+=[^=]+)/g;

    ... but you could do it with split, too, if you didn't mind a temporary array and some post-split massaging ...

    my @temp = split(/=/, $str); push @splitted, join("=", splice(@temp,0,2)) while @s;

        --k.


Re: Splitting on Every Second Occurence
by belg4mit (Prior) on May 04, 2002 at 21:04 UTC
    Not an array but would do just the same;
    %F = split /=/, $str;
    ; assuming uniqueness in keys and order doesn't matter. Just something to consider.

    --
    perl -pew "s/\b;([mnst])/'$1/g"