At 06:10 PM 7/6/2001 +0200, Aaron Craig replied to me:
<snipped out my earlier explanation>
>>use strict;
>>
>>my($file1, $f1fnum, @f1fname, $f1namef, $f1key);
>>
>>#First, get the path of the file that has the names that need to be compared.
>>#The first line prints the question, the second line assigns the typed
>>response, less the newline
>>#to a variable name.
>>
>>print "What is the path of the file that needs to be matched? ";
>>chomp($file1 = <STDIN>);
>>open(FILE1,'$file1') || die "I can't seem to find that file. Here's why:
>>$!";
>>
>>#Now, check to get information about the file itself.
>>
>>print "\nHow many fields are there in this file? ";
>>chomp($f1fnum = <STDIN>);
>>
>>print "\nPlease type the field names (no spaces) separated by commas. \n
>>For example, Name, Address1, City, etc.: ";
>
>you say no spaces, and then your example uses spaces :)
Oops.
>>chomp(@f1fname = <STDIN>);
>
>I would do this:
>my $line = <STDIN>;
>chomp $line;
>@f1fname = split(/,/, $line);
>
>and then some error checking:
>
>print "Field counts do not match!\n" if(scalar(@f1fname) != $f1fnum);
Actually, if I had been thinking in these terms, it would be easier to make
Perl count how many fields they entered, rather than having them count
it. So...
scalar(@f1fname)=$f1fnum;
might be better...
>>print "\nWhich field (1, 2, 3, 4, etc.) has the name filed to be compared? ";
>>chomp($f1namef = <STDIN>);
>>--$f1namef;
>>
>>print "\nWhich field (1, 2, 3, 4, etc.) has some unique identifier for
>>each record? ";
>>chomp($f1key = <STDIN>);
>>--$f1key;
>>
>>print "\nThat's all I need for file 1, but now I have some questions
>>about the file you will be comparing to.\n";
>>
>>my($i, %hfile1);
>>#Now, we'll need to load the file into two hashes of hashes. I know that
>>I will need to loop through line by
>>#line, so...
>>while (<FILE1>){
>>#Now, I know that I need to match each field with it's name from the
>>array to make the inner hash.
>>#I also need to then add that to the outer hash, and start with the next
>>line. I just don't know how
>>#to do that part.
I'll test this and let you know if it works.
>**** untested! ****
>my $raRecords = [];
>while(<FILE>)
> {
> my $rhNameValues = {};
> chomp;
> my @asFields = split(/,/, $_);
> if(scalar(@asFields) != scalar(@f1fname) # make sure the field
> counts line up
> {
> print "malformed line\n";
> close FILE;
> exit;
> }
> my $lCount = 0;
> foreach my $sFieldName (@f1fname)
> {
> $rhNameValue->{$sFieldName} = $asFields[$lCount];
> $lCount++;
> }
> push(@{ $raRecords }, $rhNameValue);
> }
>now you've got an array ref $raRecords with n hash refs
>$rhNameValue. Each hash ref contains a key for each field your user
>defined, with its value being the field it grabbed out of the file
>
>Aaron Craig
>Programming
>iSoftitler.com
>