At 11:53 06.07.2001 -0400, Aaron Petry wrote:
>         I have a comma delimited file like this:
>"field1","field 2","Some longer field 3 here","And some field 4"
>.I want to ask my users how many fields there are and populate that to a 
>scalar, then ask them what the field titles are and shove that input into 
>an array.  Then I want to ask them which field (asking for the number, not 
>the title) has a key value.  I want to then take that input, use it to 
>make a hash of each record, then a hash of those hashes so that I have all 
>the records.  I'm fine until that last step, and I keep thinking I've 
>worked it out, and then I run into another snag.  Here's what I have so far.

I think I know what you're trying to do :)

>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 :)

>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);

>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.
**** 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

Reply via email to