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; }

In reply to Re^3: Returning Array by muba
in thread Returning Array by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.