in reply to Newbie Trying to Read and Reformat File

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?

Replies are listed 'Best First'.
Re: Re: Newbie Trying to Read and Reformat File
by faure (Sexton) on Aug 06, 2001 at 07:01 UTC
    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... :)
Re: Re: Newbie Trying to Read and Reformat File
by tbfive (Initiate) on Aug 05, 2001 at 23:26 UTC
    Looks like a great start - Thanks!