Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Splitting on Every Second Occurence

by mt2k (Hermit)
on May 04, 2002 at 19:36 UTC ( [id://164047]=perlquestion: print w/replies, xml ) Need Help??

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"

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://164047]
Approved by mattriff
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2024-04-25 17:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found