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

Hi All, I am new to perl program, may be you have posted this earlier, but not able to find the correct solution. Here is my problem I have a file with 'n' number of lines in the format of aaaa|Q|dfadfsfaf|S|asfddsfasfdasdfaf|werwqr345435| this file delimited by either S or Q after processing this file I have to output this file like this first line should concat with the remaining aaaa|dfadfsfaf aaaa|asfddsfasfdasdfaf aaaa|werwqr345435 Any help would be helpful. Thanks Rao

Replies are listed 'Best First'.
Re: Parsing delimited file
by Corion (Patriarch) on Jan 18, 2009 at 14:34 UTC

    Hello and welcome to the monastery!

    We can only help you with your program if you show us what code you have already written and if you tell us where exactly you have problems. As your request looks as if you are basically manipulating a pipe-delimited file, maybe just using Text::xSV is the easiest way, but it's hard to tell without seeing your code.

Re: Parsing delimited file
by wilsond (Scribe) on Jan 18, 2009 at 15:46 UTC

    You can do something like this: (only kinda tested and probably not exactly what you wanted, but at least the output matches what you wanted)

    if (open(FH, "<yourdatafile.txt")) { while (my $line = <FH>) { my @columns = split(/\|/, $line); my $stage = 0; my $prefix; my @sections; my $sectionIndex = 0; foreach my $column (@columns) { next if ($column =~ m/^\s*$/); if ($column eq 'Q') { $stage++; next; } elsif ($column eq 'S') { $stage++; $sectionIndex++; next; } elsif ($stage == 0) { $prefix = ($prefix ? join('|', $prefix, $column) : $column); } else { $sections[$sectionIndex] = ($sections[$sectionIndex] ? join('| +', $sections[$sectionIndex], $column) : $column); $sectionIndex++ if ($stage == 2); } } foreach my $section (@sections) { $section = join('|', $prefix, $section); } print join(' ', @sections)."\n"; } close(FH); }

    Your question wasn't very easy to interpret. If you can clarify what you want, it would help answer your question.


    If you want to do evil, science provides the most powerful weapons to do evil; but equally, if you want to do good, science puts into your hands the most powerful tools to do so.
    - Richard Dawkins
Re: Parsing delimited file
by AnomalousMonk (Archbishop) on Jan 18, 2009 at 19:45 UTC
    As Corion mentioned, your best bet is probably one of the many fine CSV parsing modules.

    If you want to code your own solution, here is my suggestion. It will, I think (but this is untested), produce the same output as the code in wilsond's reply, but introduces the map and grep built-ins and a little more use of regexes (see perlre, perlretut and perlrequick), all of which I think you will find rewarding.

    (BTW: I, also, am afraid I do not clearly understand your requirements, but perhaps this example may yet be helpful.)

    use warnings; use strict; my $splitter = '|'; my $joiner1 = $splitter; my $joiner2 = ' '; while (my $record = <DATA>) { my ($head, @body) = grep ! m{ \A [\nSQ] \z }xms, split m{ \Q$splitter\E }xms, $record ; print join $joiner2, map join($joiner1, $head, $_), @body ; print "\n"; } __DATA__ AAAA|Q|dfadfsfaf|S|asfddsfasfdasdfaf|werwqr345435| BBBB|Q|ccccccccc|S|ddddddddddddddddd|eeeeee111111| FFFF|Q|ggggggggg|S|hhhhhhhhhhhhhhhhh|iiiiii222222|
    Output:
    AAAA|dfadfsfaf AAAA|asfddsfasfdasdfaf AAAA|werwqr345435 BBBB|ccccccccc BBBB|ddddddddddddddddd BBBB|eeeeee111111 FFFF|ggggggggg FFFF|hhhhhhhhhhhhhhhhh FFFF|iiiiii222222
      { local $/, $_ = <DATA>; s/\|$//mg && print; } __DATA__ AAAA|Q|dfadfsfaf|S|asfddsfasfdasdfaf|werwqr345435| BBBB|Q|ccccccccc|S|ddddddddddddddddd|eeeeee111111| FFFF|Q|ggggggggg|S|hhhhhhhhhhhhhhhhh|iiiiii222222|

      means the same, but probably the question means sm.th. else...
        Not quite sure what you're driving at here.

        Output:

        Parentheses missing around "local" list at ... AAAA|Q|dfadfsfaf|S|asfddsfasfdasdfaf|werwqr345435 BBBB|Q|ccccccccc|S|ddddddddddddddddd|eeeeee111111 FFFF|Q|ggggggggg|S|hhhhhhhhhhhhhhhhh|iiiiii222222