S_Shrum has asked for the wisdom of the Perl Monks concerning the following question:

I am attempting to create a cursoring subroutine that will allow me to create what I call a slice navigation string.

My script allows for a "&slice=" parameter to indicate what slice to show. This works along with my "&max=" parameter to indicate how many records to show per slice.

What I will have is a $ENV{'REQUEST_URI'} whice will contain the "slice=" string in it along with a number. There are however 3 possibilities; they are:

     FIRST PARAM: slice=[SOME_NUMBER]&this=that
MIDDLE-ISH PARAM: this=that&slice=[SOME_NUMBER]&that=this...
      LAST PARAM: this=that&that=this&slice=[SOME_NUMBER]

What I am looking for is a regex that would create a pre and post string that would do the equivilant of (using the first example:

     $pre = "slice="
     $post = "&this=that"

so that I can loop and insert numbers into my new URI

     slice=1&this=that
     slice=2&this=that
     slice=3&this=that
     ...
     slice=100&this=that
     .....

and so on, and so on. I've been trying to do this myself but have had no luck in getting a regex to address all three scenarios.

TIA

======================
Sean Shrum
http://www.shrum.net

Fixed square brackets 2002-02-27 - dvergin

  • Comment on Need to create a pre and post string from a REQUEST_URI

Replies are listed 'Best First'.
Re: Need to create a pre and post string from a REQUEST_URI
by Kanji (Parson) on Feb 28, 2002 at 04:10 UTC

    If you use CGI.pm, then there's no need to get messy with regexps: the param and (self_)url function/methods are all you need ...

    use CGI qw/ :standard /; print header; # save requested slice my $slice = param( 'slice' ); # generate URLs to next 3 slices for my $offset ( 1..3 ) { param( 'slice', $slice + $offset ); print a( { -href => self_url }, query_string ); } # restore original (if needed) param( 'slice', $slice ); __END__ Query: slice=31&this=that Output: slice=32&this=that slice=33&this=that slice=34&this=that Query: this=that&slice=49&that=this Output: this=that&slice=50&that=this this=that&slice=51&that=this this=that&slice=52&that=this Query: this=that&that=this&slice=5 Output: this=that&that=this&slice=6 this=that&that=this&slice=7 this=that&that=this&slice=8

    Season with input validation/error handling to taste. :)

        --k.

Re: Need to create a pre and post string from a REQUEST_URI
by thpfft (Chaplain) on Feb 28, 2002 at 14:02 UTC

    kanji is right, of course. CGI.pm will make your life much easier here. It deals with the problem you're facing, and with a hundred more that haven't come up yet and never will if you adopt it now...

    To answer your question, for once, here's a modified version of what i use for paging back and forth. it normally lives in a List class, so this is a bit hacked out and has all its sanity-checks removed:

    use CGI; my $query = new CGI; my $slice = $query->param('slice'); my $next_qs = build_qs($query, slice => $slice + 1); sub build_qs { my ($query, %new_param) = @_; my %parameters = map { $_ => $query->param($_) } $query->param; $parameters{$_} = $new_param{$_} for keys %new_param; return join '&', map { "$_=$parameters{$_}" } keys %parameters; }

    In this example, $next_qs will contain the query string you need to append your script address to get the next page up.

    build_qs($cgi_object, %hash) will return a query string which is exactly the same as the one supplied to the script except that any name => value pairs you supply in the %hash will be used instead of (or in addition to) the present input. the generalisation will be useful when you start wanting to change list order and length as well.

    I'm sure there's a more elegant way to do the building part - someone will probably supply a one-liner that uses CGI.pm properly - but this works for me.

Re: Need to create a pre and post string from a REQUEST_URI
by S_Shrum (Pilgrim) on Mar 01, 2002 at 07:18 UTC

    Solved it a different way in case anybody is curious. It handles all 3 scenarios:

    $pre = substr ( $ENV{'REQUEST_URI'}, 0, index ( $ENV{'REQUEST_URI'}, "slice=" ) + 6 );
    $post = substr ( $ENV{'REQUEST_URI'}, index ( $ENV{'REQUEST_URI'}, "slice=" ) + 6 );
    if ( index ( $post, "&" ) >= 0 ) { 
        $post = substr ( $post, index ( $post, "&" ) ); 
    } else { 
        $post = "";
    }

    I was expecting a convuluted regex but this seems to do the trick.

    Thanx everyone.

    ======================
    Sean Shrum
    http://www.shrum.net

Re: Need to create a pre and post string from a REQUEST_URI
by data64 (Chaplain) on Feb 28, 2002 at 02:09 UTC

    Does it have to be a regex. It seems straightforward, using "&" and then "=" to split the string.

    untested code below:

    @pairs = split( /\&/, $ENV{'REQUEST_URI'} );

    Then go through the array, split the strings on '='.

    Update:

    CrazyInsomniac pointed out that my split was not esacping the '&'


    <cite>Just a tongue-tied, twisted, earth-bound misfit. -- Pink Floyd</cite>

Re: Need to create a pre and post string from a REQUEST_URI
by screamingeagle (Curate) on Feb 28, 2002 at 04:04 UTC
    something like this might also work:
    my %FORM = (); my @QUERYARR = split(/\&/, $ENV{'REQUEST_URI'}); my $i; for $i (0 .. $#QUERYARR) { my ($key,$val) = split(/=/, $QUERYARR[$i]); $val =~ tr/+/ /; $val =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg; #this t +akes care of all the urlencoded data $FORM{$key} = $val; }
    now u can refer to your data as simple hash variables...