Gossamer Forum
Home : General : Perl Programming :

Buggrit! I'm stuck!

Quote Reply
Buggrit! I'm stuck!
Hi folks,

I've wandered into a brick wall into a subroutine in a script I'm creating.

Hokay, what I'm doing is parsing the request_uri and I want to create a clickable location bar, like on go.com.

So if my request_uri was say /subdir1/subdir2/subdir3/, I would want my navbar to look like this:

You are here --> Home > Subdir > Subdir2 > Subdir3

My code so far looks like this:

Code:
sub getlocation {
my @dirs = split("/",$ENV{REQUEST_URI});
splice(@dirs,0,3); # Ignore this, I'm just removing some extra dirs.
if (!$dirs[0]) { # If there's no first dir, I'm
$location = "Home"; # at home, so I want an unlinked "Home"
}
elsif (!$dirs[1]) { # If there's nothing after the first one
$location = ucfirst($dirs[0]); # I'm there, so I don't need a link.
}
else { # Otherwise, the first part is a linked "Home"
$location = "<a href=\"/sites/test/home/\">Home</a>";
foreach $dir (@dirs) {
# And now I'm in trouble...
# I need to set up some kind of loop and a count here to parse the rest of
# the array and create the links accordingly, excluding the last one,
# which doesn't need to be linked. Last one would be $#dirs???
}
}
}

Sorry if I'm not being clear enough, please ask me if there's anything I can clarify...

Thanks,
adam

[This message has been edited by dahamsta (edited May 20, 1999).]
Quote Reply
Re: Buggrit! I'm stuck! In reply to
Why not do it the way it is done for the Gossamer Links script? From nph-build.cgi:

Code:
sub build_unlinked_title {
# --------------------------------------------------------
# Returns a string of the current category broken up by section.
# Useful for printing in the title.

my $input = shift;
my (@dirs, $dir, $output);

@dirs = split (/\//, $input);

foreach $dir (@dirs) {
$dir = &build_clean ($dir);
$output .= " $dir:";
}
chop ($output);
return $output;
}

And from db_utils.pl (slightly modified to match what you want):

Code:
sub build_clean {
# --------------------------------------------------------
# Formats a category name for displaying.
#
my ($input) = shift;
$input =~ s/_/ /g; # Change '_' to spaces.
$input =~ s,/, > ,g; # Change '/' to ' > '.
return $input;
}

I hope this helps.

[This message has been edited by Bobsie (edited May 20, 1999).]
Quote Reply
Re: Buggrit! I'm stuck! In reply to
Does something like this help to get you started?

Code:
#/usr/bin/perl
$request_uri = "/subdir1/subdir2/subdir3/"; #simulate the uri, in the format listed as an example
$request_uri =~ s!^/!!; # Strip out the leading slash
my @dirs = split("/",$request_uri); # Split up into the different subdirectories
print "You are here --> <a href=\"/sites/test/home/\">Home</a> "; # Set the Home link
$count = 0;
foreach $dir (@dirs) {
print "> <a href=\"$dirs[$count]\">$dir</a> ";
$count += 1;
}

--Mark
Quote Reply
Re: Buggrit! I'm stuck! In reply to
Thanks lads, but...

Bobsie, your one won't work, because it's the unlinked sub, and the linked sub won't work because Links draws the url from the database. And Mark, yours won't work because the url it's creating is from root, I'm trying to build it up as I go through the array.

Anyway, I've been offline for two hours driving myself spare with it, and I eventually figured it out, but there's complications. For instance for some of the directories I have to check to see if it matches with the key of a hash, so the actual name of it is legible. If it doesn't match, I ucfirst it. But now I have so many foreach's and while's in it me head is spinning! No doubt, in the immortal words of Arnie: I'll be back! Smile

Thanks anyway lads, it's much appreciated.

adam

[This message has been edited by dahamsta (edited May 20, 1999).]
Quote Reply
Re: Buggrit! I'm stuck! In reply to
Adam,

I didn't mean to use the scripts. I meant, write your routine using the code already in those scripts. For example:

Code:
sub sub getlocation {
# --------------------------------------------------------
my @dirs = split("/",$ENV{REQUEST_URI});
splice(@dirs,0,3); # Ignore this, I'm just removing some extra dirs.
if (!$dirs[0]) { # If there's no first dir, I'm
$location = "Home"; # at home, so I want an unlinked "Home"
}
elsif (!$dirs[1]) { # If there's nothing after the first one
$location = ucfirst($dirs[0]); # I'm there, so I don't need a link.
}
else { # Otherwise, the first part is a linked "Home"
$location = "<a href=\"/sites/test/home/\">Home</a>";
foreach $dir (@dirs) {
$dir =~ s/_/ /g; # Change '_' to spaces.
$dir =~ s,/, > ,g; # Change '/' to ' > '.
chop ($dir);
}
}
}

I hope this helps.
Quote Reply
Re: Buggrit! I'm stuck! In reply to
Hiya (again) Bobsie!

Sorry, I wasn't being trappy, I appreciate ye're help. It was a right messy situation, because for one, /home/ and / are the same thing on this site. Next, I wanted Home in the Location bar, unless it was already there, and if it *was* there, I didn't want it appearing twice. I had to check from a hash to see if there was associated text, make sure the last item in the array pulled from the URI wasn't hyperlinked, etc. etc.

I think I just panicked! Smile

Anyway, I finally figured it out, except for one minor bug which doesn't bear thinking about. I now have if's, elsif's, while's and foreach's coming out me ears! But I also have a dynamic *indexable* website driven entirely by one CGI script! You want a redesign? Gimme a template and twenty minutes... Smile

Thanks anyway lads. I'm off to have the caffeine drip removed.

adam