NEw to Perl

S

Shawn Melnic

greetings,

how do I make while in perl do this:

while(&list) {
@line = ..
bla bla
bla
}

sub list {
print "line 1";
print "line 2" ;
print "line 3" ;
}

Basically, I want while to read the sub list into an array..

Thanks + +
 
D

David K. Wall

greetings,

Hello. Please put the subject of your post into the Subject of your post.
how do I make while in perl do this:

while(&list) {
@line = ..
bla bla
bla
}

sub list {
print "line 1";
print "line 2" ;
print "line 3" ;
}

Do what?
Basically, I want while to read the sub list into an array..

Whatever that means. Using PSI::ESP, I'd guess maybe you want to, um... I
think my copy of PSI::ESP is broken. What is it you wanted? Can you be more
explicit?
 
J

Jürgen Exner

Shawn said:
greetings,

how do I make while in perl do this:

while(&list) {

That's not the way to call a sub. Change that to
while (list()) {
@line = ..

Why are you using an array for individual lines?
bla bla
bla
}

sub list {
print "line 1";
print "line 2" ;
print "line 3" ;

Just add
return ("line1", "line2", "line3");
here.


jue
 
T

Tad McClellan

while(&list) {
@line = ..
bla bla
bla
}

sub list {

my @list;
push @list, "line 1";
push @list, "line 2" ;
push @list, "line 3" ;
return @list;
}

Basically, I want while to read the sub list into an array..


Can't be done.

But you can have the sub return a list, as above.
 
J

Joe Smith

Jürgen Exner said:
That's not the way to call a sub. Change that to
while (list()) {

No, change that to
foreach (list()) { ... }

[Using while() will get the three items, throw two of them away,
and loop infinitely if list() returns the same data each time.]
-Joe
 
J

Jürgen Exner

Joe said:
Jürgen Exner said:
That's not the way to call a sub. Change that to
while (list()) {

No, change that to
foreach (list()) { ... }

[Using while() will get the three items, throw two of them away,
and loop infinitely if list() returns the same data each time.]

Oooops, indeed!
Sorry.

jue
 
A

Anno Siegel

Abigail said:
Shawn Melnic ([email protected]) wrote on MMMMXXI September MCMXCIII in
<URL:`` greetings,
``
`` how do I make while in perl do this:
``
`` while(&list) {
`` @line = ..
`` bla bla
`` bla
`` }
``
`` sub list {
`` print "line 1";
`` print "line 2" ;
`` print "line 3" ;
`` }
``
`` Basically, I want while to read the sub list into an array..


sub list {
("line 1", "line 2", "line 3");
}

while (my @line = list) {
bla bla
bla
}

That's the second infinite loop to be suggested in this thread :)

Anno
 
B

Ben Morrow

Quoth (e-mail address removed):
my @list;
push @list, "line 1";
push @list, "line 2" ;
push @list, "line 3" ;
return @list;


Can't be done.

Yes it can: tie and select a filehandle to push printed stuff onto an
array. Not that I'm suggesting that's the *right* way to do it... :)

Ben
 

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

Forum statistics

Threads
474,159
Messages
2,570,884
Members
47,419
Latest member
ArturoBres

Latest Threads

Top