Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re^3: Returning Array

by muba (Priest)
on Jun 27, 2012 at 14:51 UTC ( #978684=note: print w/replies, xml ) Need Help??

in reply to Re^2: Returning Array
in thread Returning Array

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.

use strict; use warnings; use subs qw( findLines ); my @allLines = ...; for my $foundLine ( findLines(@allLines) ) { print "Found a line:\n"; print join("\n", map { "\t<$_> "} @$foundLine); print "\n"; } print pack("H*", "436f6465206279206d7562612c2066726f6d20687474703a2f2f +7065726c6d6f6e6b732e6f72670d0a0d0a"); sub findLines { my @data = @_; # @_ is the array that holds the arguments tha +t # were given when calling the sub. my @return = (); # We're going to keep an array for all the lin +es # we wanna return; # foreach $line (@data) { # Fixed. Made $line a lexical variable. --muba foreach my $line (@data) { # Combining those two if statements is something I suggested # in that thread I just linked to, at the top of this post. # It just saves a level of indentation and in my opinion it lo +oks # clearer this way. if ( ($line =~ /notice/) && ($line =~ /rdy/) ) { $line =~ s/ /,/g; # @L1 = split(/|notice|[[]|,mpmstats:,|[\t]|rdy,|bsy,|rd,| +wr,|ka,|log,|dns,|cls,/, $line); # Fixed. Made @L1 a lexical variable, and moved "[" and # "\t" out of their character classes. Something I also # suggested in the other thread. -- muba my @L1 = split(/|notice|\[|,mpmstats:,|\t|rdy,|bsy,|rd,|wr +,|ka,|log,|dns,|cls,/, $line); # Question: are you splitting here because tokens such a +s # "notice", "[", "mppstats:", etc really separate essent +ial # pieces of data, or because you just want them gone fro +m $line? # If the latter, then what's wrong with my /other/ sugge +stion in # the other thread to use s/// instead of split? # $line =~ s/|notice|\[|,mpmstats:,|\t|rdy,|bsy,|rd,|wr,|k +a,|log,|dns,|cls,//g; # # But for now I'll assume you have your reasons to split # $line, so let's roll with that. # Now the question is, how do you want to return this @L1? # Uncomment the line that does what you want. # Join the elements of @L1 into a string, using a comma +as # a separator: # push @return, join(",", @L1); # Return a reference to this @L1 so that you can still w +ork # with the individual elements. # push @return, \@L1; # Yeah, let's assume that's what you want. push @return, \@L1; } } return @return; }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://978684]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2017-01-19 09:42 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (170 votes). Check out past polls.