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

01:00:00,00:: Col Foo -- Description Scene [Sound=Dolby, Color=y, Link +=y, International=y,Budget=[total=3343;tax=11%;cost=44444]] 02:00:00,00:: Col Boo -- Description Scene [Sound=THX, Color=n, Link=y +, International=y,Budget=[total=1343;tax=1%;cost=54444]] 03:00:00,00:: Col Goo -- Description Scene [Sound=PTX, Color=y, Link=n +, International=y,Budget=[total=2343;tax=1%;cost=64444]] 04:00:00,00:: Col Aoo -- Description Scene [Sound=DDY, Color=n, Link=y +, International=y,Budget=[total=5343;tax=1.1%;cost=84444]]
I need
01:00:00,00 Col Foo Description Scene Sound Dolby Color y Link y International y Budget Total=3343 tax=11% cost=4444
And so on...

Any thoughts?

Replies are listed 'Best First'.
Re: Trying to parse columns with different delimiters.
by psini (Deacon) on May 31, 2008 at 12:54 UTC

    Yes, thanks :)

    Suggestions: try to split the problem. You don't want to parse a string with different delimiter; what you do want is to split the string into chunks using a first pattern and then split with another pattern. Try this and tell us what you can get.

    Another suggestion, read Perl Monks Approved HTML tags and please avoid <pre> tags in your future posts (it'ld be kind of you editing your original post, too) substituting <pre> with <code>

    Careful with that hash Eugene.

Re: Trying to parse columns with different delimiters.
by Corion (Patriarch) on May 31, 2008 at 13:17 UTC

    In addition to psini's suggestion of splitting the different semantics in different parts of code, you can also try to keep what you want instead of discarding what you don't want:

    # 01:00:00,00:: Col Foo -- Description Scene [Sound=Dolby, Color=y, Li +nk=y, International=y,Budget=total=3343;tax=11%;cost=44444] /^(.*? )\[Sound=(\w+), Color=([yn]), Link=([yn]), International=([yn]) +,Budget=\[total=(\d+);tax=([\d.]+);cost=(\d+)\]$/ or die "Invalid lin +e: >$_<"; print <<OUTPUT; $1 \tSound\t$2 \tColor\t$3 \tLink\t$4 \tInternational\t$5 \tBudget \t\tTotal=$6 \t\ttax=$7 \t\tcost=$8 OUTPUT

    If you're using Perl 5.10, I suggest using named captures instead of relying on the numbers to make maintenance more sensible.

Re: Trying to parse columns with different delimiters.
by BrowserUk (Patriarch) on May 31, 2008 at 14:22 UTC

    Adjust to suit your reqs:

    #! perl -slw use strict; use Data::Dump qw[ pp ]; my %data; while( <DATA> ) { chomp; m{^ (.+) :: \s+ (.+) \s+ -- \s+ (.+) \s+ \[ (.+) \] \s* }x or warn "Mismatch: '$_'" and next; $data{ $1 } = { split( ' ', $2 ), split( ' ', $3 ), map { split '=', $_, 2 } split ',\s?', $4 }; $data{ $1 }{ Budget } = { map{ split '=' } split ';', $data{ $1 }{ Budget } }; } pp \%data;

    Produces:

    { "01:00:00,00" => { Budget => { cost => 44_444, tax => "11%", total = +> 3343 }, Col => "Foo", Color => "y", Description => "Scene", International => "y", Link => "y", Sound => "Dolby", }, "02:00:00,00" => { Budget => { cost => 54_444, tax => "1%", total => + 1343 }, Col => "Boo", Color => "n", Description => "Scene", International => "y", Link => "y", Sound => "THX", }, "03:00:00,00" => { Budget => { cost => 64_444, tax => "1%", total => + 2343 }, Col => "Goo", Color => "y", Description => "Scene", International => "y", Link => "n", Sound => "PTX", }, "04:00:00,00" => { Budget => { cost => 84_444, tax => "1.1%", total +=> 5343 }, Col => "Aoo", Color => "n", Description => "Scene", International => "y", Link => "y", Sound => "DDY", }, }

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.