Gossamer Forum
Home : General : Perl Programming :

jump.cgi modification problems...

Quote Reply
jump.cgi modification problems...
I have a db-file called loopup.txt like this:
Code:
1|*
2|*
3|*
4|*
5|*

Note: Assume there is no relation between the numbers, so + 1 and -1 won't work!

Now I would like to create the following file based on this db.
Code:
1|2|5|*
2|3|1|*
3|4|2|*
4|5|3|*
5|1|4|*

The reason I would like to have such a file is very simple. Just to create back and next links on all my pages!

Hope sone can point me into the right direction!


[This message has been edited by TecLine (edited April 28, 1999).]
Quote Reply
Re: jump.cgi modification problems... In reply to
........nobody ?
Quote Reply
Re: jump.cgi modification problems... In reply to
I don't understand. Do you want to add more fields?

Pasha

------------------
webmaster@find.virtualave.net
http://find.virtualave.net
Quote Reply
Re: jump.cgi modification problems... In reply to
Pascha,

as I explained i don't want just
Code:
4|http://www.four.com
but:
Code:
4|3|5|http://www.four.com
Quote Reply
Re: jump.cgi modification problems... In reply to
How about something like:

Code:
while (<> ) {
my ($num, $url) = split /\|/;
$urls{$num} = $url;
push (@numbers, $num);
}
@numbers = sort @numbers;

for ($i = 0; $i <= $#numbers; $i++) {
print join ("|", $numbers[$i],
$numbers[$i-1],
$numbers[$i+1],
$urls{$numbers[$i]), "\n";
}

What it does is read through the file and creates a hash of number => url. It also creates an array of all your numbers. Then it sorts the numbers, and goes through each number and prints the number, the previous, the next and finally the URL. You will have to do something with the endpoints though (the first and the last numbers).

Hope that helps,

Alex
Quote Reply
Re: jump.cgi modification problems... In reply to
Alex,

I first had to change all ID numbers into 001, 002. (otherwise the sorting would not work correctly).

And about the endpoints:
I modified the following code, of the build process (detailed_pages), so the last and first detailed page would NOT have a back and next link, BUT it's not working as I would like it to work. So could you please check you piece of code on error's?

Code:
sub build_detailed_view {
# --------------------------------------------------------
# This routine build a single page for every link.
#
my (@values, $id, @records, $firstpageid, $lastpageid, %rec, $count);
if ($build_detail_path =~ m,^$build_root_path/(.*)$,) {
&build_dir ($1);
}
open (DB, "<$db_file_name") or &cgierr("unable to open database: $db_file_name. Reason: $!");
LINE: while (<DB> ) {
/^#/ and next LINE; # Skip comment Lines.
/^\s*$/ and next LINE; # Skip blank lines.
chomp;
@values = &split_decode ($_);
$id = $values[$db_key_pos];
# Now let't remember this ID, so we can sort!
push (@records, $id);
}
close DB;
# Now sort the records so we can make a detalied section, where people can
# browse with back and next links placed on every page.
@records = sort @records;
# End sorting, start determing which ID numbers are the first/last in teh array.
$firstpageid = shift @records;
$lastpageid = pop @records;
# Start building...
print "\t";
open (DB, "<$db_news_name") or &cgierr("unable to open database: $db_news_name. reason: $!");
LINE: while (<DB> ) {
/^#/ and next LINE; # Skip comment Lines.
/^\s*$/ and next LINE; # Skip blank lines.
chomp;
@values = &split_decode ($_);
$id = $values[$db_key_pos];
%rec = &array_to_hash (0, @values);
# Set back and next links.
$back_link = "/cgi-bin/jump.cgi?ID=$id&BACK=1" unless ($id == $firstpageid);
$next_link = "/cgi-bin/jump.cgi?ID=$id&NEXT=1" unless ($id == $lastpageid);
open (DETAIL, ">$build_news_detailed_path/$id$build_extension") or &cgierr ("unable to build detail page: $build_news_detailed_path/$id$build_extension. reason: $!");
print DETAIL &site_html_news_detailed (%rec);
close DETAIL;
print qq~$id ~;
(++$count % 10) or print "\n\t";
}
close DB;
print "\n";
}


[This message has been edited by TecLine (edited May 01, 1999).]
Quote Reply
Re: jump.cgi modification problems... In reply to
First, if you change your sort to:

sort { $a <=> $b }

then you won't need to change ID's to 001, as it will sort them numerically.

As for the code, did you insert my subroutine? If so, what happened?

Cheers,

Alex