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

I would like to convert a name/value text file into a column structured text file. The input file has the following name/value format (line structured):
Field_1 = value_f1_1 Field_2 = value_f2_1 ... Field_n = value_fn_1 Field_1 = value_f1_2 Field_2 = value_f2_2 ... Field_n = value_fn_2 etc...
The output file should have the following format (column structured) :
Field_1 ; Field_2 ; Field_3; ... ; Field_n value_f1_1 ; value_f2_1 ; value_f3_1 ; ... ; value_fn_1 value_f1_2 ; value_f2_2 ; value_f3_2 ; ... ; value_fn_2 etc...
Any idea how can this be done using perl ?

Replies are listed 'Best First'.
Re: How to convert a row structured text file into a column structed text file
by Fletch (Bishop) on May 04, 2007 at 16:10 UTC

    Yup, several ideas. What've you got so far?

    For questions which carry the stench of homework (as this does) it's good to at least try and give some sample of what effort you've put forth; for any question, for that matter. Show your code so far and you might get better help.

    Update: /sigh . . . or someone will come right along and do your homework for you. You lucked out.

Re: How to convert a row structured text file into a column structed text file
by Corion (Patriarch) on May 04, 2007 at 16:12 UTC

    I have to do this quite often - it's actually very easy. You collect all fields as they come in into a hash and then output the fields once you've collected a complete set, just like Pokemon.

    my @columns = qw( Field_1 Field_2 Field_3 ); my $flush = $columns[-1]; # maybe you have a better way of knowing whe +n your set is complete # output header: print join("\t", @columns), "\n"; my %row; while (<>) { if (/(\w+) = (.*)/) { $row{ $1 } = $2; if ($1 eq $flush) { print join("\t", @row{ @columns }),"\n"; }; } else { warn "Unknown line: $_"; next; }; };