I guess you are looking for something like this:

#!usr/bin/perl use strict; use warnings; use 5.010; my %data; # Parse data while (<DATA>) { chomp; my ($key, @chunks) = /(\w+ (?:\([^)]*\))?) (?: \s+ |$)/gx; next if !@chunks; foreach my $chunk (@chunks) { my ($prefix, $num, $tail) = $chunk =~ /^([a-z0-9]+)_(\d+)(.*)/ +i; $data{$key}{$prefix}{$num} = $tail; } } # Create lists of values for my $key (keys %data) { for my $prefix (keys %{$data{$key}}) { my @items = map {"${prefix}_$_$data{$key}{$prefix}{$_}"} sort {$a <=> $b} keys %{$data{$key}{$prefix}}; $data{$key}{$prefix} = \@items; } } # Generate output for my $key (sort keys %data) { my @prefixes = sort keys %{$data{$key}}; my @lines; my @colMax; while (1) { my @items = ($key, map {shift @{$data{$key}{$_}}} @prefixes); last if 1 >= grep {defined} @items; $_ //= ' -' for @items; push @lines, \@items; } for my $line (@lines) { for my $colIndex (0 .. $#$line) { my $itemWidth = length $line->[$colIndex]; $colMax[$colIndex] = $itemWidth if !$colMax[$colIndex] || $itemWidth > $colMax[$colInd +ex]; } } for my $line (@lines) { $line->[$_] = sprintf '%-*s', $colMax[$_], $line->[$_] for 0 .. $#$line; } print "@$_\n" for @lines; } __DATA__ ClusterX a_123(something) b_675(some_other_thing) b_234(something new +) c_897(some different thing) ClusterY b_6998(some_other_thing, thats new) c_877797(something diff +erent inside here) c_111(some other different thing) ClusterZ a_1234(something interesting) a_123467(something - else thats + is - interesting) 3850-1-2_12243(a new one) 3850-1-2_1789(another n +ew one)

Prints:

ClusterX a_123(something) b_234(something new) c_897(some different + thing) ClusterX - b_675(some_other_thing) - + ClusterY b_6998(some_other_thing, thats new) c_111(some other differen +t thing) ClusterY - c_877797(something differ +ent inside here) ClusterZ 2_1789(another new one) a_1234(something interesting) + ClusterZ 2_12243(a new one) a_123467(something - else thats is - +interesting)

although the final "empty" ClusterZ line is not generated.

True laziness is hard work

In reply to Re: split function problem by GrandFather
in thread split function problem by $new_guy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.