Path: utzoo!utgpu!news-server.csri.toronto.edu!clyde.concordia.ca!uunet!samsung!uakari.primate.wisc.edu!ames!uhccux!munnari.oz.au!frankland-river!pem From: pem@frankland-river.aaii.oz.au (Paul E. Maisano) Newsgroups: comp.lang.perl Subject: Re: Creating a reference to an array index Message-ID: <1362@frankland-river.aaii.oz.au> Date: 18 Mar 90 02:50:45 GMT References: <1361@frankland-river.aaii.oz.au> <1990Mar17.183039.21097@iwarp.intel.com> Organization: Australian AI Institute Lines: 50 In article <1990Mar17.183039.21097@iwarp.intel.com>, merlyn@iwarp.intel.com (Randal Schwartz) writes: > > So I still don't know why you want to alias an element like that. :-( This method probably does not buy you anything that you can't do with associative arrays or using eval and coding things a little differently. I can only give you the reason I was originally looking for a technique like this. Say I have an array of buffers, @current_buffer indexed by $current_line and I want to use it in a subroutine. I don't think it would be uncommon to see something like the following at the start: sub handle_input { local($buf) = $current_buffer[$current_line]; ... From now on I can use $buf instead of the (more informative) but extremely verbose $current_buffer[$current_line]. This is fine and good, everything works well. I have about 5 to 10 references to $buf in my subroutine which I think is easier to read than the alternative. But as soon as realize that I need to start modifying the buffer it stops working since $buf is only a copy. This is not drastic. I can either go back to using the verbose form everywhere or simply copy $buf back into the real buffer before every place I return from the subroutine. The problem seemed to me that I was using local in a different way than was intended. I was trying to use it as an aliasing mechanism which it is not. Now I can say local(*buf) = &ref($current_buffer[$current_line)]; to get the effect I was trying to achieve. For anyone interested &ref can be defined as follows: sub ref { local($tmp); grep($tmp = *_, $_[0]); $tmp; } This routine could be made more general by returning an array of references for each argument. Ie. you could write, local(*x, *y, *z) = &ref($x[1], $y[0], $z[2]); You could even set things up so that two arrays share elements!! I'd better stop right here.. I feel a madness coming on :-) ------------------ Paul E. Maisano Australian Artificial Intelligence Institute 1 Grattan St. Carlton, Vic. 3053, Australia Ph: +613 663-7922 Fax: +613 663-7937 Email: pem@aaii.oz.au UUCP: {uunet,mcsun,ukc,nttlab}!munnari!aaii.oz.au!pem