blackjudas has asked for the wisdom of the Perl Monks concerning the following question:

Ok I'm seeing the concept mentally, but alas my handicap with perl (still learning) makes me post this question: A sub calls another sub which returns an array to the original, now the original sub has an array which it tests each element for a regex pattern. If that element in the original array contains the pattern, insert the results of the other subroutine. Ok to better illustrate, here's my example:
for (@array) { if ($_ =~ /CONTENT/) { my @list = &getlist($type, $index) #... gotta find out what to do from here. } }
As you can see, I'd prefer to splice the array into the original one, the only solution that I can come up with (only one cup of coffee so far) is to concantenate each element in the internal array and assign it to $_ and splice it back in. How would you do it?

Replies are listed 'Best First'.
Re: Manipulating Arrays
by Masem (Monsignor) on Apr 03, 2001 at 22:15 UTC
    Try something like:
    my @new_array; for ( @array ) { if ($_ =~ /CONTENT/) { my @list = getlist( $type, $index ); push @new_array, @list; } else { push @new_array, $_; } } @array = @new_array;
    Or, if we want to golf it..
    @array = map { ( /CONTENT/ ) ? getlist( $type, $index ) : $_ } @array;
    (This is untested!)
    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain