sanmonkey has asked for the wisdom of the Perl Monks concerning the following question:

Greetings everyone, I have a file with the following pattern displaynum=00 value=Some0 displaynum=01 value=Some1 displaynum=02 value=Some2 value=Some3 I need to be able to display 00 Some0 01 Some1 02 Some2 Some3 The number of "values" that follow the "displaynum" varies anywhere from 1 to 4. Please provide me with some direction ! Thanks much in advance

Replies are listed 'Best First'.
Re: Processing text file
by kennethk (Abbot) on May 10, 2010 at 20:27 UTC
    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.

Re: Processing text file
by choroba (Cardinal) on May 10, 2010 at 20:46 UTC
    perl -pe 's/[^ ]+=//g'
    Or maybe you wanted to include some newlines in the input or output? Then please use the <code></code> tags around your code and data.
Re: Processing text file
by sanmonkey (Initiate) on May 10, 2010 at 21:17 UTC
    Thanks kennethk. Your code gives me good enough outline to proceed.