Yeah I see that it only returns the first line in @data. How do I return all lines? I am just printing sub findLines for now. Thank you.
| [reply] |
Replace the return with a function (push, unshift, splice, as examples) that builds up a temporary array that you return.
I would probably build it in two parts -- one that iterates over all of @data, one line at a time, and the other part that handles one line at a time. I might even put the part that iterates over @data back into the code that is calling your sub (perhaps with map or something similar).
Your code is basically selecting a group of lines, and then transforming them. To me that says something along these lines (see grep and map):
my @output = map {
doTransformation( $_ );
}
grep {
shouldSelectData( $_ );
}
@data;
| [reply] [d/l] |
Am I right in assuming this is related to Perl substitution not working?
As to answer your question... You claim to know that return EXPR returns the value of EXPR right away, without executing remaining code of the subroutine. With that knowledge in mind, it stands to reason not to return as soon as we find something, but only when we've searched everywhere.
Imagine you were tasked to search every room in your house to find out how many power sockets there are, and where they are located, and report this to your roommate/mother/significant other/teddy bear, who isn't there at the moment so you'll have to give her a phone call.
What do you do? Grab the phone as soon as you see a power socket? "Hey, yeah, it's me, AnonMonk. I found a socket in the kitchen. Bye!" Or do you go through the house systematically, keeping a list of sockets as you find them, and only grab the phone when you're confident you've covered every room. "Hey, yeah, it's me, AnonMonk. Okay, there are twenty-seven sockets: three in the kitchen, two in the bathroom, two in the master bedroom, etc etc."
So that's what you have to do here, too. Keep track of the matching lines as you run into them, and then return the whole bunch when you're done iterating over the lines.
| [reply] [d/l] [select] |