Hi,

First, thanks again for all who helped me with sorting dates with Date::Manip. That's working great.

I have another question. Below is a "sample" of a larger script. I have it working great, but I'm pretty sure the sorting and manipulation of file data is inefficient. What I'm asking is, how do I make the code more efficient? Here's the code and what it does.

Here is a sample of what the data file looks like:

CATAGORY!:CATAGORY2:CATAGORY3"CATAGORY4
3245:CATAGORY2:7:July 7,2005:data:data:data
5764:CATAGORY1:3:April 27, 2005:data:data:data
8749:CATAGOTY2:4:April 13, 2005:data:data:data
9874:CATAGORY4:7:February 10, 2006:data:data:data
0276:CATAGORY1:4:November 20, 2005:data:data:data

The script reads in the data file, the sorts all the records by the CATAGORY order found in the first line of the data file. Then each line item (now delimited by a "+") within each CATAGORY is sorted by date. After they are sorted, they are joined again by the "+" so I can display each line of data within each catagory. Here is the code:

<CODE>

my ($i, $html, @sorted);

open (DEALS, "< $pathtodatafile") || die "$!";
flock (DEALS, 2);
my @current_deals = <DEALS>;
close (DEALS);

chomp ($current_deals[0]);
my @order = split(/:/, $current_deals[0]);

#====================================================
# Sort deals into catagory and date order
#====================================================

my ($found, $count) = 0;
foreach (@order) {
 for ($i=1; $i<=$#current_deals; $i++) {
  my ($deal, $catagory, $y) = split (/:/, $current_deals[$i]);
  if ($catagory eq $_) {
   $sorted[$count] .= $current_deals[$i]."+";
   $found = 1;
  }
 }
 if ($found == 1) {
  chop ($sorted[$count]);
 }
 $count++;
 $found = 0;
}

for ($i=0; $i<=$#sorted; $i++) {
my @sorted_split_temp = split (/\+/, $sorted[$i]);

my @sorted_split_final = sort {
Date_Cmp(ParseDate((split /:/, $a)[3]), ParseDate((split /:/, $b)[3]));
} @sorted_split_temp;

$sorted[$i] = join ("\+", @sorted_split_final);

}

</CODE>

After this part of the script is executed, it is then displayed in CATAGORY order on the screen.

Again This all works nicely, but is very inefficient. I'm sure there must be a better way to write this, but this is the best I can do for now. If anyone has any suggestions, on how to make this better, I'd appreciate your input.

Thanks,

Mark



Reply via email to