in reply to Perl: Split/format the output and append it to an array

For the first part, you can use map (which takes a list of elements and returns a new one computed from elements of the first) and regexes

@xlinkpaths = map { if (/xlink (.+?) /) # if xlink has been found, and characters +captured { $1; } # yield the captured data else { (); } # yield 0 elements } @xlinks; # Do this for all elements of @xlinks
I'm aware that it's not the exact solution to your problem, you'll have to try to understand enough for you to correct it.

To concatenate two arrays (in the order you gave), just do: @exclpaths = (@xlinkpaths, @exclpaths);, or unshift @exclpaths, @xlinkpaths;, whatever looks clearer to you (if your lists are not really big, both are equivalent). See unshift or push.

In my $xlink,@xlinkpaths; the variable @xlinkpaths is not affected by the my, which will give you an error if you use strict (using strict will tell you where most of your mistakes are, so you should do it).

Replies are listed 'Best First'.
Re^2: Perl: Split/format the output and append it to an array
by GrandFather (Saint) on Sep 11, 2014 at 21:55 UTC

    For the most part if you find yourself writing significant code in a map, use a for loop instead. Your 7 line confusing map requiring multiple comments becomes:

    for my $xlink (@xlinks) { push @xlinkpaths, $1 if $xlink =~ /xlink (.+?) /; }

    Although you could instead write it:

    @xlinkpaths = map {/xlink (.+?) / ? $1 : ()} @xlinks;
    Perl is the programming world's equivalent of English
      or even
      @xlinkpaths = map /xlink (.+?)/, @xlinks;

      you think you're getting undefs (but you're not in this case)? simply prepend  grep defined,  map ...

      Well, yes, you are right. Actually I started with the short version that Anonymous Monk gave, and I thought it would be confusing for someone not used to reading that sort of things. I should have gone for your first solution when I wanted to make it more verbose.

Re^2: Perl: Split/format the output and append it to an array
by einhverfr (Friar) on Sep 15, 2014 at 04:45 UTC

    In the interest of balancing clarity and robustness with map, I would suggest the following as an example.

    map { /xlink (.+?) /; $1 ? $1 : (); } @xlinks

    Of course the trinary operator here is not needed. However, oen problem I have seen with map is accidently clobbering the return values if I am not careful. Therefore if I have a conditional in the return, I try to use a trinary operator at the very end.

    BTW, I don't mind putting significant code in a map block. However one does have to be careful about it. I think if the code gets too much, one is better off usually putting the code in a function and calling that rather than going with a for loop.