School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,213 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,564 people online right now. Registration is fast and FREE... Join Now!




Splitting a string into a list by tabs and newlines

 

Splitting a string into a list by tabs and newlines

dsteele713

15 Oct, 2009 - 08:46 AM
Post #1

New D.I.C Head
*

Joined: 15 Oct, 2009
Posts: 4

This is my second day with perl. I've worked mainly in java, ruby, and php. I know this is pretty basic, but if I have a string with a bunch of tabs and newlines, and I want to split it into a list with the token being either a \t or a \n, how would I do that?

User is offlineProfile CardPM
+Quote Post


girasquid

RE: Splitting A String Into A List By Tabs And Newlines

15 Oct, 2009 - 08:57 AM
Post #2

Barbarbar
Group Icon

Joined: 3 Oct, 2006
Posts: 1,648



Thanked: 59 times
Dream Kudos: 825
My Contributions
This is actually really easy - you just call split, using "\t" or "\n" as arguments. If you want to split on either of them, use a regular expression as your first argument to split.
User is offlineProfile CardPM
+Quote Post

dsteele713

RE: Splitting A String Into A List By Tabs And Newlines

15 Oct, 2009 - 09:02 AM
Post #3

New D.I.C Head
*

Joined: 15 Oct, 2009
Posts: 4

QUOTE(girasquid @ 15 Oct, 2009 - 08:57 AM) *

This is actually really easy - you just call split, using "\t" or "\n" as arguments. If you want to split on either of them, use a regular expression as your first argument to split.


I figured that was how you would do it, but I don't know how to make the split method take a regex.
CODE
data.split("[\t\n]")
only produces a list with one element. How should the split statement look?

This post has been edited by dsteele713: 15 Oct, 2009 - 09:02 AM
User is offlineProfile CardPM
+Quote Post

girasquid

RE: Splitting A String Into A List By Tabs And Newlines

15 Oct, 2009 - 10:26 AM
Post #4

Barbarbar
Group Icon

Joined: 3 Oct, 2006
Posts: 1,648



Thanked: 59 times
Dream Kudos: 825
My Contributions
split will take a regex if you pass it in as a regex instead of a string - like so:
CODE

split(/[\t\n]/, $string);

For more information, you may want to read the documentation on split.
User is offlineProfile CardPM
+Quote Post

dsteele713

RE: Splitting A String Into A List By Tabs And Newlines

19 Oct, 2009 - 05:01 AM
Post #5

New D.I.C Head
*

Joined: 15 Oct, 2009
Posts: 4

I just realized I've been talking about perl. I meant to say python
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 09:56PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month