Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

split question

by jrsmith (Pilgrim)
on May 16, 2000 at 17:56 UTC ( [id://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.

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

What's my password?
Create A New User
Domain Nodelet?
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?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2024-04-16 07:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found