PDA

View Full Version : I need to make a record description global


NiteOwl
09-04-2006, 11:10 PM
I need a way to maintain this nightmare.

I have a dozen scripts that have this code and whenever i add a new data field, say after $vt, i have to search a dozen scripts and change them all.
I need a way to make my record desc global and use the indivual fields as needed.

The data is stored in different txt files that use "|" as a delimitor.

All txt files have the same record structure.





$filename = "somefile.txt";
$idnum = param(listnum);
&readdata;
&processdata;


sub readdata {
open(INF,"$filename") or &dienice("Can't open $filename: $! \n");
flock(INF, LOCK_SH); # shared lock
seek(INF, 0, SEEK_SET); # rewind to beginning
my(@data) = <INF>;
close(INF);
@listingdata=@data;
}


sub processdata {
my @data = sort(@listingdata);
foreach my $i (@data) {
chomp($i);
my $city,$state,$price,$bed,$bath,$sqfeet,$title,$desc,$caption,$list,
$type,$status,$zip,$acre,$agent,$vt) = split(/\|/,$i);

if ($idnum eq $list) {
#Do Various prints and compares

print "Desc: $desc";

if ($status < 2) {
# do this
}

# End $list compare
}


# End Processdata
}




Thanks for looking..
:eek:

KevinADC
09-05-2006, 01:11 AM
Changes in the actual data structure are always a pain in the arse. There really is no way to get around the problem that I am aware of. Maybe Fish will have a suggestion worth considering. On another note, this line is bad:

my $city,$state,$price,$bed,$bath,$sqfeet,$title,$desc,$caption,$list,
$type,$status,$zip,$acre,$agent,$vt) = split(/\|/,$i);

There is no '(' after 'my' and before $city. Should be:

my ($city,$state,$price,$bed,$bath,$sqfeet,$title,$desc,$caption,$list,
$type,$status,$zip,$acre,$agent,$vt) = split(/\|/,$i);

FishMonger
09-05-2006, 01:33 AM
I can show you several methods for accessing the data in a more generic/general way, but no matter which method you use, if you add extra data fields, then you'll also need to alter the scripts to be able use those extra fields.

My first suggstion would be to use the DBI and DBD::CSV modules to access the data file with sql statements as if it were an actual relational database.

But your main problem is that if you need to add additional data fields, then that shows that the db file wasn't planned out properly in the first palce. My best recomendation would be to rethink and redesign the database and use a REAL relational database such as mysql. Another option would be to write a module that reads the flat file and returns the data as a reference to an array of hashes.

In the end you need to deside, is it better to "maintain this nightmare" or redesign it from the ground up.

NiteOwl
09-05-2006, 01:59 AM
[QUOTE=KevinADC]
Yep...
it must have been deleted during my copy/paste. I checked, it does exist in the actual code.

Good Catch, Kevin. Thank You!
:thumbsup:

NiteOwl
09-05-2006, 02:12 AM
This was my first big CGI project. What i know about Perl/CGI, I learned on an "as needed basis". It keeps growing like a weed.

Thanks for your help Fish Monger.

rwedge
09-06-2006, 05:07 AM
I need a way to make my record desc global and use the indivual fields as needed.You can use 'require' to add the sub to the different files, giving you one to edit. You should be able to use individual fields as need.