It looks like most of the earlier replies have missed your point that each line of input contains multiple "tag value" pairs. So you need a loop over each input line:
my %sequences; # this will be a hash of arrays while (<DATA>) { @fields = split; for ( my $i=0; $i<@fields; $i+=2 ) { push @{$sequences{$fields[$i]}}, $fields[$i+1]; } } for my $tag ( sort keys %sequences ) { my $i = 0; for my $val ( @{$sequences{$tag}} ) { printf( "%s [ %d ] : %s\n", $tag, $i+1, $sequences{$tag}[$i++] + ); } } __DATA__ A 100 B 200 C 400 A 150 C 250 D 550 B 350 A 200 B 300 C 500 A 600 B 700 C 800 D 900
Now, you didn't actually say how you want to handle the cases where the same tag occurs on multiple lines of input (like in the sample data provided here): should the index numbers reset to 1 for each line, or should they increment continuously over the entire input stream (as done here)?

Some of the stuff you said in a sub-reply to merlyn seemed not to make sense:

A $$ indicates the start of the record. The record is split on the tag. A hash is populated with key being the tag and value being the data. Some fields within a record repeat in a well defined manner.
If I understand that, splitting the record into a hash would be completely wrong: it only lets you keep one value for each distinct tag on a line, no matter how many times that tag appears.

I hope the snippet provided here makes sense -- note that it keeps all the values for each tag in an array that is stored as the value of the hash element for that tag. Then, we just use the array index (plus 1) to provide the sequence numbers that you want for each of the values.


In reply to Re: Generating sequence nos. for data by graff
in thread Generating sequence nos. for data by icg

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.