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

Dear Monks

I have to convert the file from one format to another format. Source file contain free form text, and I have to extract the values for the fields, massage them and put in the destination file. For each line of entry in the source file there should be corresponding line in the destination file except source file contain certain things. There are some values in sourefile which is also used in the lines other than where they are etc etc.
Sample data

Source:|A|1|    ** ABC -XYZ.CO 
Destination: |A|1| ** (C) ABC
Thus XYZ.CO is converted into C and also removed etc..
Question: How should I set my classes and structures to perform the convesion if decided the object oriented way.
Currently I have
while(<IN>) $line = new Format($_); $line->extractCompanyStuff; $line->convertCompanyStuff; $line->placeCompanyStuff_inDestinationFormat $line->printToDestination }
There are 10-15 stuff like replaceCompanyStuff, and they are not this simple. They have variety of source and destination format etc, may be I am looking for something better.

Thanks
Seeking art of structured conversion
an artist

Replies are listed 'Best First'.
Re: Format Conversion Structure
by BrowserUk (Patriarch) on Sep 10, 2002 at 00:46 UTC

    I read your question several times, but I couldn't understand quite what is was that you were asking. That you have had no other replies probably means that others are having similar difficulties.

    Try posting a little more information, maybe the actual code as you have it now. The sample you give is both incorrect (missing open curly brace after while(<IN>), and so generic and isolated that it is impossible (for me anyway) to see what you are attempting.


    Well It's better than the Abottoire, but Yorkshire!
Re: Format Conversion Structure
by Zaxo (Archbishop) on Sep 10, 2002 at 01:43 UTC

    Why do you believe treating lines as objects will help?

    You have a seemingly arbitrary data translation to make. A hash would seem to solve that:

    my %translator = ( '-XYZ.CO' => '(C)', # etc ); while (<IN>) { chomp; print OUT join( ' ', map { $_->[0], $_->[1], $translator{ $_->[3] }, $_->[2]; } [split ' ', $_]), $/; }

    It is difficult to make a more concrete recommendation with seeing the variance in your data.

    After Compline,
    Zaxo