Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

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?

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
[erix]: my objection to sigs is just that they take screen space. ( Some monks would have *several* slogans underneath a 1-liner reply )
[Discipulus]: a solution would be disabiltate sig as default and let users to show them
[karlgoethebier]: now it's getting weird...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (10)
As of 2017-06-22 12:42 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (519 votes). Check out past polls.