Reading a scalar line by line

A

AcCeSsDeNiEd

Hi, does anyone know how I would do this?

something like:

while ($string)

print _$
}

Google searches did not turn up anything.

Thanks


To e-mail, remove the obvious
 
T

Tore Aursand

AcCeSsDeNiEd said:
Hi, does anyone know how I would do this?

Do what?
something like:

That's not good enough. Tell us excactly _what_ you want to do, and
we'll help you. Lucky you, your subject told us a little.
while ($string)

print _$
}

No wonder. Try splitting your $string on line breaks (if that _is_ what
you want to do);

while ( split(/\n/, $string) ) {
print $_;
}

Why do you keep separate lines in a scalar anyway? I find it handier to
keep them in a list.


--
Tore Aursand <[email protected]>

"Whenever I see an old lady slip and fall on a wet sidewalk, my first
instinct is to laugh. But then I think, what if I was an ant, and she
fell on me. Then it wouldn't seem quite so funny." -- Jack Handey
 
B

Brian Harnish

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi, does anyone know how I would do this?

something like:

while ($string)

print _$
}

I believe you are looking for something like this "print $_ for(<>)" for
strings instead of files.

print $_ for(split(/\n/, $string));
or
for(split(/\n/, $string)){
print $_;
}

Or, by "something like" do you mean non-working code? Because _$ doesn't
exist.
To e-mail, remove the obvious
Obvious to who? Is removing just "SpamMinus" supposed to be obvious? or
just "Spam"?

- Brian
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)

iD8DBQE/VNasiK/rA3tCpFYRAlgzAKDoOVCLkRQO2e779WtDZACYUPUMjwCg1FqN
VN+OSr1EsddJ0MeZjtzQX7k=
=Jinw
-----END PGP SIGNATURE-----
 
J

John W. Krahn

Tore said:
Why do you keep separate lines in a scalar anyway? I find it handier to
keep them in a list.

You can't KEEP anything in a list. Perhaps you meant an array? :)

perldoc -q "What is the difference between a list and an array"



John
 
A

AcCeSsDeNiEd

while ( split(/\n/, $string) ) {
print $_;
}

That didn't work. Looks like it was going into a continuous loop.

Why do you keep separate lines in a scalar anyway? I find it handier to
keep them in a list.

Scalar was provided by a module.


To e-mail, remove the obvious
 
A

AcCeSsDeNiEd

I believe you are looking for something like this "print $_ for(<>)" for
strings instead of files.

print $_ for(split(/\n/, $string));
or
for(split(/\n/, $string)){
print $_;
}


Yes! Thanks that did the trick
Or, by "something like" do you mean non-working code? Because _$ doesn't
exist.

Yes that was non-working code.
Ehh....but I made a typo with '_$'.
Sorry, been away from perl pretty long.

Obvious to who? Is removing just "SpamMinus" supposed to be obvious? or
just "Spam"?

The first one's right.
Sorry I don't state exactly what 'obvious' means because a few 'email harvesters' are known to read
the body of the message, and look for stuffs like 'remove....' and you know the rest of the story.
And why not? We could easily design one in Perl :)

To e-mail, remove the obvious
 
A

AcCeSsDeNiEd

First, there is no such thing as _$. You must mean $_. Now to answer your
question:

Sorry, it's just that I've been away from perl for sometime
Got enough perl books.
Do this:

foreach ( split /\n/, $string ) {
print;
}

Yes that works too. Thanks.


To e-mail, remove the obvious
 
S

Sam Holden

That will never terminate unless $string was empty.
Perhaps you meant:

for (split /\n/ => $string) {
...
}

That would work, but it takes potentially a lot of memory if $string
is huge.

I would do:

while ($string =~ /(.*)/g) {
print $1;
}

The body of the loop will get slightly different data then the split version.

Since (.*) will match the empty string between the text of the line and
the newline character (well something like that anyway, extra empty values
get passed to the loop body in $1 anyway).
 
A

Anno Siegel

Sam Holden said:
The body of the loop will get slightly different data then the split version.

Since (.*) will match the empty string between the text of the line and
the newline character (well something like that anyway, extra empty values
get passed to the loop body in $1 anyway).

Here's a variant that avoids this:

print "$1\n" while $multi =~ /(^.*)/mg;

Anno
 

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,135
Messages
2,570,783
Members
47,341
Latest member
hanifree

Latest Threads

Top