in reply to Reinventing wheels: query string parsing.

It seems like you will inspect each 'd=' part twice. After finding an 'o=' thing, you start with a new index, $j = $i + 1, scanning for 'd=' thingies. If you find one or more, you increment $j, but not $i. Personally, I'd make two nested loops, both of the form while (@query_string), and both shifting off elements.

Also, it seems that if you have a string of the form:

o=foo&d=one&d=two&o=foo&d=three

you just end up with foo => 'three'.

Personally, I would write the final assignment as:

$args {$o} = \@opts;

so that my values are always array refs, and I don't have to make cases in the rest of the code.

Abigail

Replies are listed 'Best First'.
Re: Re: Reinventing wheels: query string parsing.
by BUU (Prior) on Jul 27, 2003 at 21:03 UTC
    >> It seems like you will inspect each 'd=' part twice...
    Yeah, I think your right. To be honest I hadn't really looked at optimizing the code very much, I played around a bit with just setting $i to $j after the inner while loop finishes, but that didn't appear to change the speed one whit ( Probably needs more benchmarks there ) and I was a little uncertain of what exactly I might break by doing that so I took it back out.

    >>you just end up with foo => 'three'
    Hrm. Thats a case I hadn't thought of. Hm. Now I can't decide exactly what should happen. I'm favoring the idea of turning it into an array of arrays so your case o=foo&d=one&d=two&o=foo&d=three would turn in to $args{o}=[[one,two,three],[three]] only problem is that seems kind of icky, especially the derefencing but I like the idea of being able to call a certain option multiple times.

    >>Personally, I would write the final assignment as:
    Well, I considered that idea but my mind rebelled against the idea of having to write $arg{foo}->[0] every single time I wanted to access a variable. I'm thinking I'll just leave it how it is at the moment, operating under the assumption that most options will only accept one scalar and if a certain option needs multiple values or can accept them then it will check.