map typically returns the number of elements that are in your original list. How does map return fewer elements? --By using a test and upon failure return an empty list, "()" because undef, 0 (zero), or leaving off the empty list all create extra elements in the list.

I recently had a need to splice an array into 4 element array refs. Here's an example implementation:

# use CGI because it has subs that operate on array refs
use CGI qw/:standard/;

use Data::Dumper;
use strict;

my @list = (1..7);
print "list: ", Dumper(\@list),"\n";

# set the number of elements for the splices
my $num_elements = 4;


my @newlist =

    # This map operates on the list of splices.
    # If you remove the empty list "()" extra
    # empty elements will appear in your new list.
    #
    # CGI::Tr( $array_ref ) is used for demonstration purposes.
    map { Tr($_) }

    # This map operates on the spliced elements.
    #
    # CGI::td( $array_ref ) is used for demonstration purposes.
    map { td( [ splice(@list,0,$num_elements) ] ) }

    0 .. (@list/$num_elements);

print "newlist: ", Dumper(\@newlist),"\n";

__END__
    
>perl example.pl
list: $VAR1 = [
          1,
          2,
          3,
          4,
          5,
          6,
          7
        ];

newlist: $VAR1 = [
          '<tr><td>1</td> <td>2</td> <td>3</td> <td>4</td></tr>',
          '<tr><td>5</td> <td>6</td> <td>7</td></tr>'
        ];

Update: Changed the list from @_ to 0 .. (@_ / $num_elements) and removed some extra error checking from the top map. Thank you Aristotle for your suggestions.

$num_elements = 4; @_ = ( list,of,elements,to,splice ); @spliced_list = # This map operates on the list of splices. # the left side of || should test something # most likely something in $_ and return # something (or call a sub) if the test is # successful, and if the test fails, it should # failover to the right side of the || which # returns (). map { func1($_) } # This map operates on the spliced elements. # Splicing the list removes those elements # from the list as a side affect. map { func2( [ splice(@_, 0, $num_elements) ] ) } # Since this snippet shows how to fold an array, # we only need to iterate over the count of # new elements. 0 .. (@_ / $num_elements);

In reply to Fold a list using map splices by hiseldl

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.