On Mon, Dec 31, 2007 at 05:56:35PM -0500, Jean-Rene David wrote:

> I wonder what idioms are available to loop through
> the lines stored in a scalar variable. I guess I'm
> looking for something analogous to these idioms
> for files and arrays respectively:
> 
> while(<FH>) {
> # do stuff
> }
> 
> foreach (@array) {
> # do stuff
> }
> 
> When I had to do this I split the scalar in an
> array:
> 
> @array = split "\n", $scalar;
> foreach (@array) {
> # do stuff
> }
> 
> What would be some other ways to do this? (This is
> purely curiosity.)

The most direct analogy would be to use an in-memory file:

open my $fh, "<", \$scalar;
print while <$fh>;

-- 
Paul Johnson - [EMAIL PROTECTED]
http://www.pjcj.net

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to