Forum Moderators: coopster & phranque

Message Too Old, No Replies

how to translate env variables

a regex catch 22

         

idiotgirl

10:28 pm on Nov 13, 2001 (gmt 0)

10+ Year Member Top Contributors Of The Month



Okay, let's just ignore, "But why would you even bother?", assume I'm nuts and impractical, and please advise if there's a solution to my problem:

I'd like to incorporate this into a script:

GET chosen page's $ENV{'DOCUMENT_URI'} and $ENV{'DOCUMENT_NAME'}, translate "/" to "_", and use that value for another function.

Chosen page would have an exec cgi call on the page to this script, similar to a counter.cgi

So, we take a DOCUMENT_URI/ DOCUMENT_NAME of:
foo/subfoo/index.html

and change it to:
foo_subfoo_index.html

$NEWVARPATH = "foo_subfoo_";
$NEWVARHTML = "index.html";

This seems so simple, until I hacked away at it for four or five hours. I'm either making it harder than it needs to be, or ignoring necessary regexes. Either way, I'm sunk.

Any recommendations?

littleman

11:14 pm on Nov 13, 2001 (gmt 0)



You are on an MS box right? Otherwise you'd be able to use $ENV{'REQUEST_URI'} which would have everything you need and just convert the '/'s to '_'s and leave it at that. You could try:
$newenv ="$ENV{'DOCUMENT_URI'}$ENV{'DOCUMENT_NAME'}";
$newenv =~ s/\//_/g;

idiotgirl

11:17 pm on Nov 13, 2001 (gmt 0)

10+ Year Member Top Contributors Of The Month



Sorry, I should have said I'm on a unix/apache.

idiotgirl

11:22 pm on Nov 13, 2001 (gmt 0)

10+ Year Member Top Contributors Of The Month



So far, $ENV{'DOCUMENT_URI'} includes the full path, including the document's name. But I want to split those so I have a path and a document name. Because some directories may be:

(home) /index.html
foo/subfoo/index.html
foo/subfoo/subofsubfoo/other.html

I want to be sure I can split this all up so the paths and document names can be separated.

So far, I have trouble because anything in the home directory just shows as:

/index.html

and gives me no path. Even:

_index.html

would help.

toadhall

7:54 pm on Nov 16, 2001 (gmt 0)

10+ Year Member



I'm not much of a Perl coder, but perhaps you can translate this PHP:

<?

$str = "foo/subfoo/index.html";

// get the position of the last slash:
$pos = strrpos($str, "/");

// increment the variable to go one character beyond the slash:
$pos2 = ++$pos;

// pick off the file name and file extension:
$file = substr($str, $pos2);

// grab the path (from zero to the position of the last slash):
$path = substr($str, 0, $pos);

// change the slashes to underscores:
$path = ereg_replace("/", "_", $path);

echo ("$path<br>\n"); // returns foo_subfoo_
echo ("$file"); // returns index.html

?>

Perhaps there's an easier way, but this works.

idiotgirl

9:41 pm on Nov 16, 2001 (gmt 0)

10+ Year Member Top Contributors Of The Month



php? Well, its worth a look. Since *I'm* not much of a Perl coder, either (even though I do it) this could take a little work. I'll get to it tonight and let you know if I can translate. Unfortunately, php is not an option for me, as the rest of the scripts are all Perl.

But, I think the add / then strip routine is what it will take to make this work. that's where my problems have been.

mdharrold

12:04 am on Nov 17, 2001 (gmt 0)

10+ Year Member



I was working on this a few days ago. This is what worked for me.

#!/usr/local/bin/perl
#######Split the various parts of the request page
@new = split(/\//, $ENV{REQUEST_URI});
#######Get the original page name
$NEWVARHTML = $new[-1];
#######Remove the first and last elements leaving only the directories requested
pop @new;
shift @new;
#######Take the directories requested and add the "_" to the end
foreach $new (@new)
{$PATH{$new} = "$new\_";}
########Get only the values for the new path
@NEWVARPATH = values(%PATH);

print "Content-type: text/html\n\n";
print "<br />NEWVARHTML = $NEWVARHTML";
print "<br />NEWVARPATH =@NEWVARPATH";

exit(0);

I tested it with no directories, one directory, and two directories. It worked fine for all of them so it should work beyond that too.

idiotgirl

12:30 am on Nov 17, 2001 (gmt 0)

10+ Year Member Top Contributors Of The Month



mdharrold-

Ohmygosh! You mean you read the original post and decided to start hacking for the heck of it... or you're weird like me and decided you really needed a script to do this? It's so much nicer when you know you aren't alone :)

I'm debugging something else at the moment ,<473 lines down with about 300 to go>, but I'm eager to see what flies later and let you know. Thanks so much for sharing.

mdharrold

3:18 am on Nov 17, 2001 (gmt 0)

10+ Year Member



I thought I needed a script to do this but I don't remember what the final goal was so I stopped.

idiotgirl

9:36 am on Nov 17, 2001 (gmt 0)

10+ Year Member Top Contributors Of The Month



mdharrold-

Well, it works like a champ. Now I have my paths and pagename's split up I can automatically call all kinds of files based on the document folder and/or filename. Works slick as can be. I had it working when I was blindly hacking away at it - except for the home (base) folder wasn't working. Nothing would show up unless it was a doc in a subfolder. This fixed it.

Thanks a bunch.