Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Extract data from pipe delimited text

by Anonymous Monk
on Jul 19, 2002 at 14:58 UTC ( #183259=perlquestion: print w/replies, xml ) Need Help??

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

category1 category1|subcat1 category1|subcat1|subcat1 category1|subcat1|subcat2 category1|subcat1|subcat3 category1|subcat2 category1|subcat2|subcat1 category2
how would I just pull out the categories, "category1 category2", would I have to do somehing like !~ /^+.?\/

Replies are listed 'Best First'.
Re: Extract data from pipe delimited text
by Joost (Canon) on Jul 19, 2002 at 15:05 UTC
    #!/usr/bin/perl -w use strict; while(<DATA>){ chomp; next unless /^([^\|]+)/; print "$1\n"; } __DATA__ category1 category1|subcat1 category1|subcat1|subcat1 category1|subcat1|subcat2 category1|subcat1|subcat3 category1|subcat2 category1|subcat2|subcat1 category2
    What does this have to do with MySQL?
    -- Joost downtime n. The period during which a system is error-free and immune from user input.
Re: Extract data from pipe delimited text
by simeon2000 (Monk) on Jul 19, 2002 at 16:21 UTC
    If I remember correctly (which doesn't always happen), I believe Mysql STDOUT query output (which is what you, I believe, are parsing), uses "\s+|\s+" in between columns. Unless you are just talking about a flat file with pipe as the delimeter that you can THEN slap into mysql, wherein I believe

    while (my $line = <>) { my $category = split("|", $line, 1); # ... }
    would get the job done.
Re: Extract data from pipe delimited text
by Nightblade (Beadle) on Jul 19, 2002 at 15:04 UTC
    m/^(\w+)\|(\w+)/; $cat1 = $1; $cat2 = $2;

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2023-05-31 02:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?