Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Parsing pipe-delimited text that contains pipes

by slugger415 (Monk)
on Aug 09, 2019 at 23:44 UTC ( [id://11104245]=perlquestion: print w/replies, xml ) Need Help??

slugger415 has asked for the wisdom of the Perl Monks concerning the following question:

Might be a dumb question, but is there a module or some easy method to parse pipe-delimited text strings that might actually contain a pipe character?

Yes it's easy to use split to split up a string, but my text contains some real pipe characters. It uses the Excel-type formatting where double-quotes surround the whole string that contains the pipe text, e.g.

"A bunch of pipes: ||| right here"|next column|last column

I can bend my brain trying to do this with regex's but then I'd have to account for other characters such as double quotes, and I'm betting someone has figured this out already. I know CSV parsers can handle this sort of thing, maybe there's a CSV parser that allows different delimiters?

thanks!

Replies are listed 'Best First'.
Re: Parsing pipe-delimited text that contains pipes
by AnomalousMonk (Archbishop) on Aug 10, 2019 at 00:38 UTC
    I know CSV parsers can handle this sort of thing ...

    You're right: Text::CSV and Text::CSV_XS (update: the latter maintained by our own Tux).


    Give a man a fish:  <%-{-{-{-<

Re: Parsing pipe-delimited text that contains pipes
by soonix (Canon) on Aug 10, 2019 at 12:32 UTC
    AnomalousMonk implied it, I tell you explicitly: both Text::CSV and Text::CSV_XS can handle vertical-bar-separated-values, the options to control this behaviour are sep_char and sep.
Re: Parsing pipe-delimited text that contains pipes
by karlgoethebier (Abbot) on Aug 10, 2019 at 12:41 UTC

    I guess you want to set sep_char from Text::CSV to something useful. See also Re: Transpose a file. Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

    perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

Re: Parsing pipe-delimited text that contains pipes
by daxim (Curate) on Aug 10, 2019 at 07:10 UTC
Re: Parsing pipe-delimited text that contains pipes
by slugger415 (Monk) on Aug 12, 2019 at 18:49 UTC

    Another (dumb) q (sorry, CSV newbie here): I can see how to use Text::CSV to parse files, but how do I parse an array? All I see is while (my $row = $csv->getline ($fh)) from a $fh file handle.

      I can see how to use Text::CSV to parse files, but how do I parse an array?
      $csv->parse($string) or die "failed to parse '$string'"; my @fields = $csv->fields;

      Update: I assume you meant how to parse a string instead of an array. If not, please show (with code!) what you mean.

        Yes that was what I meant (an array of strings) and was looking for, thank you!

Re: Parsing pipe-delimited text that contains pipes
by slugger415 (Monk) on Aug 12, 2019 at 17:55 UTC

    So easy, thanks all!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-04-24 23:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found