Welcome to WebmasterWorld Guest from 18.208.159.25

Forum Moderators: coopster & jatar k & phranque

Message Too Old, No Replies

CGI Sorting Problems

Sorting script problems.

     
9:29 pm on May 12, 2003 (gmt 0)

New User

10+ Year Member

joined:May 14, 2003
posts:29
votes: 0


I'm working on a web page in which houses are sorted by price. However, when there are two houses the same price, only one is displayed on the page when it's sorted. I can't see any errors in the scripting! Can anyone help? :)
9:38 pm on May 12, 2003 (gmt 0)

Administrator

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 24, 2001
posts:15756
votes: 0


Welcome to WebmasterWorld lisad,

I am thinking that it may stem from the sql query (I assume it is stored in a db). How about a look at the actual query you are using.

4:09 pm on May 13, 2003 (gmt 0)

New User

10+ Year Member

joined:May 14, 2003
posts:29
votes: 0


I am not using a database. The files that are being sorted are HTML files. The programmer that created this file had said,

"There was no template for the script but I can recommend checking to see if the file is lost when it gets put in the hash table or if it gets lost in the sort. In perl there are three types of variables variants, arrays, and hash tables represented by these characters $, @, % respectively. In the script I used two hash tables (%variableName) to organize the way the home flite’s would be displayed. I would recommend you run tests on a machine with apache installed and test the script to see where the file is getting lost by Outputting the contents of the hash tables to the screen. Without seeing everything though I would have to recommend looking at the html file first."

But he is no longer able to work on the page...

Also, I'm unfamiliar with apache. How do I begin testing scripts?

1:54 am on May 15, 2003 (gmt 0)

Preferred Member

10+ Year Member

joined:Mar 2, 2003
posts:398
votes: 0


Lisad: try this-

House No. 1: $ 120,000

House No. 2: $ 120,001

Would that make sense?

:-)

2:30 am on May 15, 2003 (gmt 0)

Preferred Member

10+ Year Member

joined:Aug 25, 2001
posts:467
votes: 0


Sorting is a big pain in the neck in Perl. IMO, you're really going to need some help from a programmer. Sorry.
3:19 am on May 15, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Mar 27, 2003
posts:664
votes: 0


"...you're really going to need some help from a programmer..."

And you have come to the right place!

But you will have to give us some clues...

You indicated

"...I can't see any errors in the scripting..."
but then again your ex-programmer suggested a few things
"...recommend checking to see if the file is lost when it gets put in the hash table or if it gets lost in the sort ... test the script to see where the file is getting lost by Outputting the contents of the hash tables to the screen..."
What were the results of the tests, or don't you know how to do what he was asking?

Shawn

PS Not sure I'd agree that "...Sorting is a big pain in the neck in Perl...". Perl provides better support for sorting than most languages. It has the built-in 'sort' function. Couple taht with with perl's regex functionality, and you're on a winner where sorting is concerned. And if that isn't enough, there are lots of library out there too. In any language, algorithms for sorting huge amounts of data very efficiently can be complex, but that 'sort' of problem (pardon the pun) is unlikely to come up in run-of-the-mill web development. When it does come up, another language may be chosen instead of perl, but not because perl falls short in it 'sort' functionality. Rather because the problem will require a compiled language with more sophisticated data structures instead of a scripted one like perl or php.

8:30 pm on May 19, 2003 (gmt 0)

New User

10+ Year Member

joined:May 14, 2003
posts:29
votes: 0


I did some tests using the perl executable (but that pretty much just tests for syntax, which is fine). I just haven't been able to figure out using apache to test it. It is installed on my computer, and I've tried to run tests, but I'm not sure where I'm supposed to store the .cgi file. Also, there is an HTML page that calls the script, and I'm not sure where to store that either.

P.S. Perl seems like a good language, but I'm just not advanced enough yet to make sense of my problem. (Logically I do, but when it comes to actually fixing it, I'm in the dark.)

5:52 pm on May 20, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:May 5, 2003
posts:80
votes: 0


You mentioned a hash, I am wondering what your key values are. Could be that the key value being used is duplicated for two houses with the same price, thus the first is over-written by the second.

For example:


%hash = ('one' => 1, 'two' => 2, 'three' => 3);

This would make $hash{'one'} output 1, $hash{'two'} output 2 and so on. Now let's take it further.


$hash{'four'} = 4;
$hash{'five'} = 5;
$hash{'three'} = 6;

Now what you would have is 1,2,6,4,5 as values in the hash.

I know this is a simplistic example, but this could be the problem.

Scott Geiger

6:09 pm on May 20, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 19, 2003
posts:695
votes: 0


If you don't have more than 100 or so items to sort, you could just do an old fashion bubble sort.
7:25 pm on May 20, 2003 (gmt 0)

New User

10+ Year Member

joined:May 14, 2003
posts:29
votes: 0


I'm having the toughest time with this problem. I don't think that the hash value is being overwritten. Here is most of the code..
_____________________________________________________________
HEADER

@priceRange = split /-/, $contents{'priceRange'};
$low = $priceRange[0];
if ( $priceRange[1] eq "+" )
{
$priceRange[1] = 100000000;
}
$high = $priceRange[1];

$i = 0;

opendir(BIG, "../homeflite/");
@filenames = grep(/\.html$/,readdir(BIG));
closedir(BIG);

foreach $file (@filenames)
{
$fileContents{$file} = get( "http://www.stuff.com/homeflite/" . $file );
&setPrice;
$SortPrice{$tmpPrice} = $file;
}

foreach $key (sort keys %SortPrice)
{
push @sortedFiles, $SortPrice{$key};
}

$last = shift @sortedFiles;
push @sortedFiles, $last;

foreach $file (@sortedFiles)
{

$contents = $fileContents{$file};

$imageFile = substr($file, 0, index($file, ".") );

&getMetaVariables;

if ( $#price <= 1 )
{
$price = $price[0] * 1000;
$price += $price[1];
}
elsif ( $#price == 2 )
{
$price = $price[0] * 1000000;
$price += $price[1] * 1000;
$price += $price[2];
}

if ( $price >= $priceRange[0] && $price <= $priceRange[1] )
{
&outputHTML;
}
elsif ( $contents{'priceRange'} eq "all" )
{
&outputHTML;
}

}

print "</center>\n";
print "</td>\n";
print "</tr>\n";
print "</table>\n";
print end_html;

sub setPrice()
{

chomp($fileContents{$file});
@testHTML = split /\n/, $fileContents{$file};
$tmp = substr( $testHTML[2], index($testHTML[2], chr(34) ) + 1, length($testHTML[2]) );
$tmp = substr( $tmp, 0, index($tmp, chr(34) ) );
@data = split /-/, $tmp;
@tmpRice = split /,/, $data[5];

if ( $#tmpRice <= 1 )
{
$tmpPrice = $tmpRice[0] * 1000;
$tmpPrice += $tmpRice[1];
}
elsif ( $#tmpRice == 2 )
{
$tmpPrice = $tmpRice[0] * 1000000;
$tmpPrice += $tmpRice[1] * 1000;
$tmpPrice += $tmpRice[2];
}

}

sub getMetaVariables()
{

chomp($contents);
@testHTML = split /\n/, $contents;
$tmp = substr( $testHTML[2], index($testHTML[2], chr(34) ) + 1, length($testHTML[2]) );
$tmp = substr( $tmp, 0, index($tmp, chr(34) ) );
@data = split /-/, $tmp;
@price = split /,/, $data[5];

}
________________________________________________________

7:43 pm on May 20, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:May 5, 2003
posts:80
votes: 0


Ok without knowing what the data in the files looks I am making some guesses.


&setPrice;
$SortPrice{$tmpPrice} = $file;

What if you have 2 files with the same $tmpPrice. The second data set will over-write the first based on this.

e.g.:
file_a has a $tmpPrice = 200000
file_b has a $tmpPrice = 250000
file_c has a $tmpPrice = 200000

then where $tmpPrice = 200000; $sortPrice{$tmpPrice} = file_c. file_a is over-written.

Scott Geiger

8:06 pm on May 20, 2003 (gmt 0)

New User

10+ Year Member

joined:May 14, 2003
posts:29
votes: 0


How would I go about fixing this?
11:26 pm on May 20, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:June 9, 2002
posts:41
votes: 0


You can try to change this

foreach $file (@filenames)
{
$fileContents{$file} = get( "http://www.stuff.com/homeflite/" . $file );
&setPrice;
$SortPrice{$tmpPrice} = $file;
}

foreach $key (sort keys %SortPrice)
{
push @sortedFiles, $SortPrice{$key};
}

to:


foreach $file (@filenames)
{
$fileContents{$file} = get( "http://www.stuff.com/homeflite/" . $file );
&setPrice;
$SortPrice{$file} = $tmpPrice;
}

my @sorted = sort {
$SortPrice{$a} <=> $SortPrice{$b}
} keys %SortPrice;

foreach $key (@sorted)
{
push @sortedFiles, $key;
}

2:34 pm on May 21, 2003 (gmt 0)

New User

10+ Year Member

joined:May 14, 2003
posts:29
votes: 0


That code worked but now the sorting is screwed up and the very first entry (at the cheapest price) is being displayed last, after the most expensive house. How do I fix that?
12:54 pm on May 22, 2003 (gmt 0)

New User

10+ Year Member

joined:May 14, 2003
posts:29
votes: 0


I think that the $SortPrice{$a} <=> $SortPrice{b} line in this paragraph:
________________________________________________________
my @sorted = sort {
$SortPrice{$a} <=> $SortPrice{$b}
} keys %SortPrice;
________________________________________________________

is what causes the first entry to be pushed to the last entry, but I need it to be displayed first. How do I rewrite that line to display the first entry first? I've tried altering the line a little bit, but I haven't had any luck...

Please help!

1:45 pm on May 22, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Mar 27, 2003
posts:664
votes: 0


The line you refer to is the 'test' which is applied to each pair of elements in the array during the sort, so if the only problem is the last entry, and the rest are sorted correctly, you are looking in the wrong place. If the whole table is sorted largest to smallest instead of the other way round, just swap the a and the b.

To help further it would be helpful to have further information, such as the contents of the SortPrice hash, and any other debugging results.

Shawn

9:15 pm on May 22, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:June 9, 2002
posts:41
votes: 0


That code worked but now the sorting is screwed up

what do you mean by that?

and the very first entry (at the cheapest price) is being displayed last, after the most expensive house. How do I fix that?

I have no idea why your programmer did it, but here is the problem:

$last = shift @sortedFiles;
push @sortedFiles, $last;

you can try to comment this two line out.

3:21 pm on May 23, 2003 (gmt 0)

New User

10+ Year Member

joined:May 14, 2003
posts:29
votes: 0


You guys are lifesavers! Thank you so much!