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 |
In reply to Re: Parsing delimited file
by wilsond
in thread Parsing delimited file
by ravula1
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |