I figure that your thinking is too complicated, especially when it comes to regex and processing the input lines.
Consider this code:
#!/usr/bin/perl use strict; use warnings; while (my $line = <DATA>) { next if $line =~ /^\s*$/; # skip blank lines $line =~ s/^\s*//; # remove leading spaces $line =~ s/\s*$//; # remove trailing space and line ending + my ($name, @nums) = split /[\s-]+/, $line; foreach my $num (@nums) { print "$name\t$num\n"; } } # PRINTS #abcd 723 #abcd 724 #abcde 552 #abcde 554 #abcde 553 #abcdef 756 __DATA__ abcd 723-724 abcde 552-554-553 abcdef 756
I don't think the trim leading and trailing spaces statements are needed here given your DATA. However, you should become familiar with how to do that.

Update: As a general rule:

This code works the same:
#!/usr/bin/perl use strict; use warnings; while (my $line = <DATA>) { next if $line =~ /^\s*$/; # skip blank lines my ($name, @nums) = $line =~ /(\w+)/g; foreach my $num (@nums) { print "$name\t$num\n"; } } # PRINTS #abcd 723 #abcd 724 #abcde 552 #abcde 554 #abcde 553 #abcdef 756 __DATA__ abcd 723-724 abcde 552-554-553 abcdef 756
I almost always have a statement to throw away blank lines. They can often appear at the end of a file and hard to see when you just type or cat the file.

In reply to Re: regex problem by Marshall
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.