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

Problem splitting a string that contains ^I

by Anonymous Monk
on Apr 21, 2002 at 21:34 UTC ( #160930=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a string with ^I in between. I want to split the string by ^I. I tried all possibilities. Nothing helped me. Can you help me.

Comment on Problem splitting a string that contains ^I
Re: Problem splitting a string that contains ^I
by dws (Chancellor) on Apr 21, 2002 at 21:38 UTC
    The tab (^I) characters is represented in Perl strings via \t. Thus   @parts = split "\t", $string;
    will separate tab-delimited parts.

      split with '\t' has helped me. Thank you very much.
Re: Problem splitting a string that contains ^I
by belg4mit (Prior) on Apr 21, 2002 at 21:38 UTC
    UPDATE: Doh! See dws's reply above. Damn sans-serif font...

    Examining the split documentation very closely we see that the first argument is in fact a regular expression. So you are trying to match a pipe at the beginning of the line. You need to escape your sequence. This could easily have been found by RTFM, and perhaps by a search BUT, it is something that even experienced people get nicked by. Enjoy.

    --
    perl -pew "s/\b;([mnst])/'$1/g"

Re: Problem splitting a string that contains ^I
by Fletch (Chancellor) on Apr 21, 2002 at 21:41 UTC

    • perl -le '$_="a\tb\tc";print join( "\n", split( /\t/ ) )'
    • If you've got a literal ^ followed by a literal I (i.e. you don't mean a TAB character), keep in mind that split takes a regex and ^ has a special meaning therein.
    • If you're trying to parse something like a CSV file exported from Excel or the like, consider Text::CSV_XS.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (7)
As of 2014-09-02 02:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (18 votes), past polls