What have you tried? How hasn't it worked? Please see How do I post a question effectively?. In particular, given that you are trying to read input from a file, it is important that we can see how the file is formatted. This can be accomplished by wrapping the input file contents in <code> tags. The same should be done for your expected output.

The simplest file data structure that matches your spec as I understand it is a hash of arrays. See perllol for some background on these structures. The following code demonstrates some of the fundamentals required to process input in the way you've described:

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $input = <DATA>; my @data = split /\s+/, $input; my $last_display; my %results; for my $datum (@data) { my ($key, $value) = split /=/, $datum, 2; if ($key eq 'displaynum') { $last_display = $value; } if ($key eq 'value') { push @{$results{$last_display}}, $value; } } print Dumper \%results; for my $key (keys %results) { print join "\t", $key, @{$results{$key}}, "\n"; } __DATA__ displaynum=00 value=Some0 displaynum=01 value=Some1 displaynum=02 valu +e=Some2 value=Some3 __END__ $VAR1 = { '01' => [ 'Some1' ], '00' => [ 'Some0' ], '02' => [ 'Some2', 'Some3' ] }; 01 Some1 00 Some0 02 Some2 Some3

If this does not provide a clear road map for how to proceed, please post some code and well-formatted input and expected output and we'll see what we can do.


In reply to Re: Processing text file by kennethk
in thread Processing text file by sanmonkey

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.