Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

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
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 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.

    cheers
    sc
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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (8)
As of 2015-07-04 16:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (60 votes), past polls