I would recommend to first read all of your data into a datastructure, like a hash of hashrefs with the names as the keys and then process the data according to your logic:

use strict; use warnings; use Data::Dumper; my @extract; while(<DATA>){ if /^\.EON/ or /^\s*$/; push @extract, {name => $1} and next if /^\$ NAME : (\w+)/; ${$extract[-1]}{$1} = $2 if /(\S+)\s*(.*)/; } my %result = map { delete $_->{name} => $_ } @extract; print Dumper \%result; __DATA__ $ NAME : corry $$.Inc s d $$.Oc s $$.TO G1 ty n1 EE EE M T1 T2 $$SRU G2 n1 y OO OO M T3 T4 $$SRU .EON $ NAME : patrick $$.Inc c d $$.Oc c $$.TO G1 td n3 EE EE M T5 T6 $$SRU G2 n3 y OO OO M T7 T8 $$SRU .EON $ NAME : sandy $$.Inc k l $$.Oc l $$.TO G1 td n3 FF FF M R5 R6 $$SRU G2 n3 y OO OO N R7 R8 $$SRU .EON

In reply to Re: How to extract certain range of text and write into another file? by hdb
in thread How to extract certain range of text and write into another file? by annel

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.