Re: foreach routine
- Posted by Chris Bensler <bensler at nt.net> Dec 22, 2006
- 674 views
duke normandin wrote: > > c.k.lester wrote: > > > > FMI, what is the benefit of a foreach statement vs. for x = y to z? I've considered foreach in the past for various language designs and preprocessor features. I could see no real gain and it creates issues when one also wants access to the iterator in a foreach loop, which is often enough that a foreach construct is too specific to be a good addition to Euphoria. > > Would there be a performance benefit on the backend? Not likely, possibly a very tiny one. > Hi... > > I'm too new to euphoria code to venture even a guess, never mind an opinion. > Coming from Perl, I'm trying to emulate the following: > > read(STDIN, $buffer, $ENV{CONTENT_LENGTH}); > @pairs = split(/&/, $buffer); > foreach $pair (@pairs){ > ($name, $value) = split(/=/, $pair); > blah > blah > } > > I'm going to use a `for' loop to `getc' CONTENT_LENGTH amount of chars and > shove them into `sequence buffer'. > > I've got a split routine that I think will work. > > I just need to walk through the 'sequence pairs' one at a time, split > again and stash each in there own sequence. > > Like I said in a prior post -- sequences are so HUGE of a deal in Euphoria > that the support for them ought to be a non-issue. As it stands now, the hoops > that I *think* I'll have to jump through to do anything with sequences are > bizarre. Maybe this is *one* of the issues that needs to be fixed in order > to make a powerful language that much more attractive and efficient to use. > > BTW... TP80-cgilib.e ! Still available?? Later.... > -- > duke Here is some Eu code to do what you want (split() function sold separately) Warning: code is not tested, consider it a guideline only
include get.e -- needed for get_bytes() and value() sequence pairs,pair sequence name,data -- ('value' is a standard routine name, so I will use 'data' instead) sequence buffer object tmp -- temporary/intermediate variable tmp = getenv("CONTENT_LENGTH") if atom(tmp) then -- CONTENT_LENGTH was not found else tmp = value(tmp) if tmp[1] != GET_SUCCESS then -- value() failed else buffer = get_bytes(STDIN,tmp[2]) pairs = split(buffer,'&') for i = 1 to length(pairs) do pair = pairs[i] tmp = split(pair,'=') name = tmp[1] data = tmp[2] -- blah -- blah end for end if end if
Chris Bensler ~ The difference between ordinary and extraordinary is that little extra ~ http://empire.iwireweb.com - Empire for Euphoria