Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!timbuk!shamash!rrr From: rrr@u02.svl.cdc.com (Rich Ragan) Newsgroups: comp.lang.perl Subject: Proposed extension Keywords: arrays perl Message-ID: <29428@shamash.cdc.com> Date: 20 Dec 90 20:59:24 GMT Sender: usenet@shamash.cdc.com Reply-To: rrr@svl.cdc.com Organization: Control Data Corporation, Silicon Valley Operations Lines: 63 I keep needing to get at the current index of the iterator. For example, the following piece of code from a mail gateway moves the user who was being processed during a crash to the front of the process list on the next run by swapping with the current first user. The folks array has their name and the signons array has the info to sign on the user with the mail system. # Move interrupted user to the first entry in the list. for ($i=0; $i <= $#folks;$i++) { if ($folks[$i] eq $interrupted_user) { ($signons[0], $signons[$i]) = ($signons[$i], $signons[0]); ($folks[0], $folks[$i]) = ($folks[$i], $folks[0]); last; } } Generally, I hate to code for loops with explicit loop counters because its easy to be off by one on the termination test [You can't mess up foreach (@folks) ]. For the sake of discussion, I will invent some syntax which is likely not to be just what one would want to use but all the $special-chars have pretty much been gobbled up. In the revised example @. has the value of the current index of the iterator array. # Move interrupted user to the first entry in the list. for (@folks) { if ($_ eq $interrupted_user) { ($signons[0], $signons[@.]) = ($signons[@.], $signons[0]); ($folks[0], $folks[@.]) = ($folks[@.], $folks[0]); last; } } Access to the current iterator index would seem to be useful for other common things such as "the previous element" or "the next element" that I have seen asked about in this group. My example above would be further simplified by enhanced support for arrays. In particular, a grep-like array search but one that returned the index rather than the value of the first match. Thus it becomes, if (($loc = locate(@folks, $interrupted_user))+1) { ($signons[0], $signons[$loc]) = ($signons[$loc], $signons[0]); ($folks[0], $folks[$loc]) = ($folks[$loc], $folks[0]); } An extension of this would return an array of element locations that matched the string. This could be used to access the interesting elements. This could be further extended by allowing the match string to be a regexp. Any thoughts on these ideas. I suspect that strengthening the array features of perl will improve its power and speed (fewer things to interpret per unit of work done) -- * / \ / o \ Seasons Greetings, /o o\ Richard R. Ragan rrr@svl.cdc.com (408) 496-4340 -------- Control Data Corporation--Silicon Valley Operations ||