[plug] [OT] how to do this in perl?

Ryan ryan at is.as.geeky.as
Thu Aug 12 10:13:47 WST 2004


On Thu, 2004-08-12 at 09:55, simon at plumtek.com wrote:
> Hi all
> 
> just a quick question - If I have a for loop in perl, how do I affect the
> 'parent' $_ variable from within it? Within the loop the $_ variable is equal
> to the counter.... but I want to append a string to $_ variable outside the
> loop....
> 
> Any clues?

No.  "I don't get it" :P

If you want to access the loop counter outside the loop, what is the
point of that once you are outside the loop?

.. or are you calling a subroutine from within the loop and want to
access $_ in that sub?  In which case you'd pass it as a parameter.

Could you provide some existing code or some pseudo-code so we can see
what you mean?

The $_ is also read only in a loop context.  You might also have more
luck using a C style for loop rather than the shorthand (x..y) style as
it will let you modify the loop variables at will.  I don't know how
useful that is until I know what you are trying to do however :)

You can also force the counter to increment with 'next' and an
appropriate conditional block.

This article may help you a bit also:

http://tlc.perlarchive.com/articles/perl/rl0004.shtml

Ryan




More information about the plug mailing list