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

Oh wise ones... My situation is this - I have a text file that is stored in a name / value format that looks something like this:

Company: ABC Trucking
Contact: Joe Smith
Title: President
etc...

I want to convert it to a pipe delimited file, or something similar. The problem is that each record does not have the same # of fields (for example, one may have just Company and Title). I am looking for a way to read through the file, separate each field with some special character and if the record does have one of the fields (from the know full list of fields) append a space to the field. Please let me know if anyone can point me to any examples or sources of information that can help me solve my problem. Thanks a bunch!
  • Comment on Newbie Trying to Read and Reformat File

Replies are listed 'Best First'.
Re: Newbie Trying to Read and Reformat File
by chromatic (Archbishop) on Aug 05, 2001 at 22:49 UTC
    That's not hard, if you're clever. I'd set up a hash for each record, initialized to default values. Something like this:
    my @records; my @fields = qw( Company Title Phone Address Contact ); # read in a record at a time { my %record; @record{@fields} = ('') x @fields; foreach my $element ($line) { my ($field, $value) = split(/:\s*/, $element); $record{$field} = $value; } push @records, \%record; }
    Then to print, loop through @records, loop through the keys or values of each %record, and print them with a join. Make sense?
      Drat! Chromatic already showed you the hashslice!

      That's my favorite... anyway here is my belated version of the same thing--

      #!/usr/bin/perl -w use strict; my @fields = qw[Company Contact Title Phone]; for my $file (@ARGV) { open TEXT, "< $file" or die "Couldn't open $file: $!\n"; my %contact; while(<TEXT>) { chomp; my ($k, $v) = split ": " or next; $contact{$k} = $v; } # hashslice print join("|", @contact{ @fields }), "\n"; }
      Just watch out for people with pipes in their names... :)
      Looks like a great start - Thanks!
Re: Newbie Trying to Read and Reformat File
by tachyon (Chancellor) on Aug 05, 2001 at 22:40 UTC

    Have a look at Converting GEDCOM Files for some good code examples on how you rework data files. chipmunk gave a very slick Perl solution and I gave a more plodding baby perl reply. You will find all the basic code you need there.

    You do not specify what separates your records which is usually vital to know. Is it say a blank line as speculated upon by tye or something else. A sample of two records of input and two records of the desired output would help. Preferably also post the code you are having a problem with.

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Newbie Trying to Read and Reformat File
by petdance (Parson) on Aug 06, 2001 at 04:25 UTC
    Why are you trying to convert it to "a pipe delimited file, or something similar"? What are you going to do with it then? Get it into Excel? Access? Some other system?

    If so, you may do well to go directly to that system, and not go to the intermediate text file stage. Give us a little more background on your project and we may be able to provide more insight.

    xoxo,
    Andy
    --
    <megaphone> Throw down the gun and tiara and come out of the float! </megaphone>