foreach handling of multiple arrays in perl4.35 
Author Message
 foreach handling of multiple arrays in perl4.35

Hi,
I just came accross a bug in my code, which I have since fixed, but I
was wondering if this really should be the case.

In the Camel book, it says something like: If you change the contents
of $_ in a foreach loop, you can change the content of the array being
'foreach'ed over. OK, from that I got a loop looking like this:


        {
        s,^,$curr_rel_path,;
        }

Then, I wanted to do EXACTLY the same operation on another array

me:


        {
        s,^,$curr_rel_path,;
        }

And suddenly NO (effective) substitutions were done!!

Ok, in this case the body of the loop is pretty small and easy to
duplicate, however, later in the code, I again want to process both
arrays, in ALMOST identical ways, the difference being the contents of



which array is currently being processed, or even only if it were
possible to know when foreach changed from one array to the next. (The
position within the array would also be nice, but I can keep my own
loop counters). I now have the code duplicated (about 20 lines per
loop) which is pretty ugly, and its not the sort of thing which
warrants splitting out into a subroutine.

Anyone care to comment on these observations????

Steve



Mon, 29 Jul 1996 22:54:52 GMT  
 foreach handling of multiple arrays in perl4.35

...


:        {
:        s,^,$curr_rel_path,;
:        }
:
:And suddenly NO (effective) substitutions were done!!

...

:Anyone care to comment on these observations????

From the manual:

     If ARRAY is an  actual array  (as  opposed  to  an  expression
     returning  an array value), you can modify each element of the array
     by  modify- ing VAR inside the loop.

The key point is that while

is a real, honest-to-goodeness, names array,

is merely an array expression.  

Nonetheless, you'll find that this *DOES* work in perl5!  Cool.

--tom
--

      "Will Hack Perl for Fine Food and Fun"
        Boulder Colorado  303-444-3212



Tue, 30 Jul 1996 22:39:54 GMT  
 foreach handling of multiple arrays in perl4.35

Quote:


> ...

> :        {
> :        s,^,$curr_rel_path,;
> :        }
> :
> :And suddenly NO (effective) substitutions were done!!
> :Anyone care to comment on these observations????
>...
> Nonetheless, you'll find that this *DOES* work in perl5!  Cool.

And of course, it works in Perl 4 if you use grep() instead of foreach:


--

"When the only tool you have is Perl, the whole | "Hooray for snakes!"
 world begins to look like your oyster." -- Me  |  -- The Simpsons (29 Apr 93)
------- "We _love_ being politically Koreshed" -- Steve Taylor, "Smug" -------



Sat, 03 Aug 1996 04:17:48 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. perl4.35 core dump

2. difference in substr($_,34,35) vs substr($_,034,35)

3. using multiple array fields in foreach

4. Can I foreach multiple arrays?

5. perl 4.0.35 on solaris 2.3

6. BUG/Limitation: Parser stack overflow in eval (4.0 pl 35)

7. Compiling Perl-4.35 on Apollo Domain 4.0 ?

8. patchlevel 35 on SCO 3.2.4 fails tests

9. Kerberized Perl PL 35 ???

10. Bug in patch level 35

11. Problem compiling perl@35 on SunOS 4.0.3

12. Perl 4.0.35 for VMS/Posix

 

 
Powered by phpBB® Forum Software