Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Problem splitting a string that contains ^I

by Anonymous Monk
on Apr 21, 2002 at 21:34 UTC ( [id://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

Replies are listed 'Best First'.
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 Fletch (Bishop) 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.

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"

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
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?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2024-04-18 15:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found