http://www.perlmonks.org?node_id=183259

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 Nightblade (Beadle) on Jul 19, 2002 at 15:04 UTC
    m/^(\w+)\|(\w+)/; $cat1 = $1; $cat2 = $2;
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.