in reply to Help? - Trouble outputting data corectly

In my revision of your code, below, the essential change is that each data record read in is stored as a hash, and each of these hashes is put on a list (actually, in an array). This is much cleaner (and much less error prone) than having a separate array for each input field.

In order to support your purpose, I've taken it one step further, and made a separate array of records for each neighborhood. There is one top-level data structure, the hash %m. Its keys are the values for the neighborhood field from each of the lines read in. The corresponding values are arrays of records, where each record is a hash, as explained above.

And lastly, one other change, which I think makes the code much neater, and also somewhat more efficient: each line of input from the files is processed as it is read in, rather than slurping them up into an array and then stepping over the array.

Oh -- and I made it use here docs instead of multiple print statements.
my @fields = qw( num address image neighborhood modelname modelelevation bedrooms +bathrooms typeoffoundation schooldistrict price paymentsfrom readydate phon +enumber email ); my %m; # key: neighborhood name. value: array of records. { open FILE, "< $datafile" or &fatal_error("Unable to open $data +file"); &lock(FILE); while ( <FILE> ) { chomp; my %rec; @rec{@fields} = split /``/; # put each record into the list for that neighborhood: push @{ $m{ $rec{'neighborhood'} } }, \%rec; } &unlock(FILE); close FILE; } open FILE, "< $template" or &fatal_error("Unable to open $template +"); &lock(FILE); while (<FILE>) { open FILE, "< $template" or &fatal_error("Unable to open $template +"); &lock(FILE); while (<FILE>) { if (/<!--messages-->/) { print <<EOF; <table width=400 border=0 cellpadding=3 cellspacing=3> <tr class="typewhite" bgcolor="#000000"> <td><b>Address</b></td> <td><b>Model Name</b></td> <td><b>Price</b></td> <td><b>Availability</b></td> </tr> EOF foreach $neighborhood ( sort keys %m ) { print <<EOF; <tr class="type" bgcolor="#EEEFDD"> <td colspan=4> <a href="../cristohomes/hoods/$neighborhood.shtml" cla +ss="type"> <b>$neighborhood</b> </a> </td> </tr> EOF for my $rec ( @{ $m{ $neighborhood } } ) { print <<EOF; <tr class="type"> <td> <a href="$indexcgi${queryswitch}action=display&num=$r- +>{'num'}" class="red"> $r->{'address'} </a> </td> <td> <a href="../cristohomes/homes/$r->{'modelname'}.shtml" + class="red"> "The $r->{'modelname'}" </a> </td> <td>$r->{'price'}</td> <td>$r->{'readydate'}</td> </tr> EOF } } print "</table>\n"; } } &unlock(FILE); close FILE;

jdporter
...porque es dificil estar guapo y blanco.

Replies are listed 'Best First'.
Re: Re: Help? - Trouble outputting data corectly
by endsinister (Initiate) on Dec 17, 2002 at 16:05 UTC
    Thank you so very much! All of my problems have been solved!