A solution using splits and maps to build a hash. The order of the output might be problematic if you desire something other than sorted.

johngg@shiraz ~/perl/Monks $ perl -Mstrict -Mwarnings -E ' open my $inFH, q{<}, \ <<EOD or die $!; abcd 723-724 abcde 552-554-553 abcdef 756 EOD my %hash = map { $_->[ 0 ] => [ split m{-}, $_->[ 1 ] ] } map { [ split ] } <$inFH>; foreach my $key ( sort keys %hash ) { say qq{$key $_} for @{ $hash{ $key } }; }' abcd 723 abcd 724 abcde 552 abcde 554 abcde 553 abcdef 756

I hope this is useful.

Update: This version dispenses with the hash so items will be output in the same order as input.

johngg@shiraz ~/perl/Monks $ perl -Mstrict -Mwarnings -E ' open my $inFH, q{<}, \ <<EOD or die $!; abcd 723-724 abcde 552-554-553 abcdef 756 EOD say qq{@$_} for map { my $key = $_->[ 0 ]; my @nos = split m{-}, $_->[ 1 ]; map { [ $key => shift @nos ] } 1 .. scalar @nos; } map { [ split ] } <$inFH>;' abcd 723 abcd 724 abcde 552 abcde 554 abcde 553 abcdef 756

Update 2: Even simpler.

johngg@shiraz ~/perl/Monks $ perl -Mstrict -Mwarnings -E ' open my $inFH, q{<}, \ <<EOD or die $!; abcd 723-724 abcde 552-554-553 abcdef 756 EOD say qq{@$_} for map { my( $key, $rest ) = split; my @nos = split m{-}, $rest; map { [ $key => shift @nos ] } 1 .. scalar @nos; } <$inFH>;' abcd 723 abcd 724 abcde 552 abcde 554 abcde 553 abcdef 756

Cheers,

JohnGG


In reply to Re: regex problem by johngg
in thread regex problem by Anonymous Monk

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.