So I see the plot thickens...

I made a straightforward modification to previous code to account for the fact that you have pairs of things instead of single space separated things in the input data. I am confused by your last example output line 10GBE_ADDR1 (ANALOG:107) U212.19. I just assumed that this was a cut-n-paste error? If not, then you have a lot more explaining to do about "what the rules are".

I am not sure if this is what you need, but we are incrementally closer...

#!/usr/bin/perl use warnings; use strict; while (my $line = <DATA>) { next if $line =~ /^\s*$/; #skip blank lines my ($label, @rest) = split ' ', $line; my @pairs; while (@rest) { my $first_num_thing = shift @rest; my $paren_thing = shift @rest; push @pairs, "$first_num_thing $paren_thing"; } @pairs = sort @pairs; #may need special sort?? foreach my $col (@pairs) { print "$label $col\n"; } } =prints 10GBE_ADDR1 R3629.2 (ANALOG:107) 10GBE_ADDR1 R3633.1 (ANALOG:107) 10GBE_ADDR1 U212.19 (INPUT:107) =cut __DATA__ 10GBE_ADDR1 R3629.2 (ANALOG:107) R3633.1 (ANALOG:107) U212.19 (INPUT:1 +07)
Now of course in your "real" code vs my "demo" code, use something more descriptive that "$paren_thing". I am sure in your actual context that thing has some name or description that makes a lot more sense than that!

I hope that you have read my previous answer to your questions and that this post makes more sense to you now. As with the previous code post, this is "runnable code" as is.

What I expect you to do is use my code as a starting point. Play with it. Modify it. I am trying to provide enough info to get you "unstuck". You need to start writing some code yourself. There are of course other ways to write this code. I attempted to be straightforward and not overly fancy.

Update:
Ok, I will demo another technique. If you can understand how both of these programs work, then you are well on your way. Split and "match global" can solve an enormous percentage of file parsing problems.

#!/usr/bin/perl use warnings; use strict; while (my $line = <DATA>) { next if $line =~ /^\s*$/; #skip blank lines my ($label, $rest) = split ' ', $line,2; (my @pairs) = $rest =~ /(\S+\s+\S+)/g; #called "match global"; @pairs = sort @pairs; foreach my $col (@pairs) { print "$label $col\n"; } } =prints 10GBE_ADDR1 R3629.2 (ANALOG:107) 10GBE_ADDR1 R3633.1 (ANALOG:107) 10GBE_ADDR1 U212.19 (INPUT:107) =cut __DATA__ 10GBE_ADDR1 R3629.2 (ANALOG:107) R3633.1 (ANALOG:107) U212.19 (INPUT:1 +07)

In reply to Re^3: Perl Formatting Text by Marshall
in thread Perl Formatting Text by oopl1999

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.