Loop through scalar?

J

Jason Kinkade

I have a scalar/variable ($var) that looks like:

Line1
Line2
Line3
Line4
etc.

Theres obviously a new line after each Line#. I would like to loop
through this variable line by line with something like

foreach ($var)
{
print "Current Line: $_";
}

This reads the entire $var in. I want it line by line instead.

Any idea?
Thanks.
 
A

Anno Siegel

Jason Kinkade said:
I have a scalar/variable ($var) that looks like:

Line1
Line2
Line3
Line4
etc.

Theres obviously a new line after each Line#. I would like to loop
through this variable line by line with something like

foreach ($var)
{
print "Current Line: $_";
}

This reads the entire $var in. I want it line by line instead.

It prints it out. No reading here.
Any idea?

See perldoc -f split

Anno
 
J

Jürgen Exner

Jason said:
I have a scalar/variable ($var) that looks like:

Line1
Line2
Line3
Line4
etc.

Theres obviously a new line after each Line#. I would like to loop
through this variable line by line with something like

foreach ($var)
{
print "Current Line: $_";
}

This reads the entire $var in.

Last time I checked prin() would print, not read but maybe this has changed
I want it line by line instead.
Any idea?

So you want to split() (hint, hint!) the string into several pieces?

jue
 
J

Joe Smith

Jason said:
Theres obviously a new line after each Line#. I would like to loop
through this variable line by line with something like

Learn how to use split().
-Joe
 
J

Jeff 'japhy' Pinyan

It prints it out. No reading here.

Jason meant the foreach loop "reads" all of $var as one element; he had
hoped it would split it on newlines.

--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
Senior Dean, Fall 2004 % have long ago been overpaid?
RPI Corporation Secretary %
http://japhy.perlmonk.org/ % -- Meister Eckhart
 
J

John W. Krahn

Abigail said:
Unlike others in this thread, I wouldn't use split, but m//g:

while ($var =~ /(.*\n)/g) {
print "Current line: $!";
}

So you are saying that $! contains the current line? :)


John
 
M

Michele Dondi

Theres obviously a new line after each Line#. I would like to loop
through this variable line by line with something like

foreach ($var)
{
print "Current Line: $_";
}

BTW: curious indenting style!

Well, there should be nothing to add to what others already suggested
you and most definitely split() is the way anyone would reasonably go.

However, more as of a curiosity than anything else I'll mention that
you can also do


#!/usr/bin/perl

open my $fh, '<', \ <<"EOTXT" or die "D'Oh!";
line1
line2
line3
EOTXT

s/\d+$//,print while <$fh>;

__END__


But then (in your case) please *don't* do this... ;-)


Michele
 
E

Eric J. Roode

Unlike others in this thread, I wouldn't use split, but m//g:

while ($var =~ /(.*\n)/g) {

Out of curiosity, why?

--
Eric
`$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=(
$!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++;
$_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++
;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=`
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,427
Latest member
HildredDic

Latest Threads

Top