Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

split question

by jrsmith (Pilgrim)
on May 16, 2000 at 17:56 UTC ( #11841=perlquestion: print w/ replies, xml ) Need Help??
jrsmith has asked for the wisdom of the Perl Monks concerning the following question:

is there any way to retain the character you are splitting by when you do a split()? say i have the string "4sheet1rev11" and i want to split it on each number.. problem is it deletes the number in the process, giving me-
sheet rev
when i want-
4 sheet 1 rev 11
can anyone help me here?

Comment on split question
Select or Download Code
Replies are listed 'Best First'.
Re: split question
by snowcrash (Friar) on May 16, 2000 at 18:07 UTC
    put the delimiter (here: one or more digits) in parentheses, that also returns the delimiters:
    my $str = "4sheet1rev11"; my @arr = split(/(\d+)/, $str);
    note, that you have an empty string at the beginnig of the array, since your string starts with a digit.

Re: split question
by jrsmith (Pilgrim) on May 16, 2000 at 18:04 UTC
    well i found a solution... simple parentheses around the split exp.. thanx anyway tho :)
Re: split question
by lhoward (Vicar) on May 16, 2000 at 18:09 UTC
    I know you can do it with a regular-expression. I'm not sure if you can with a regular split.
    my $s='4sheet1rev11'; while($s=~/([\d]+|[^\d]+)/gcsi){ print "$1\n"; }

    Just saw the other comments. It can be done w/ split.

    That's what I love about perl... there are always many ways to solve any problem and I'm always learning something new.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://11841]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2015-10-10 00:33 GMT
Find Nodes?
    Voting Booth?

    Does Humor Belong in Programming?

    Results (254 votes), past polls