This bit is quite wrong:

my @names = map {$i > 6 ? push(@names,$input2arr[$i]) : $i++ } @input2 +arr;

Inside the map block (i.e. the bit between { and }) you don't just put the equivalent of what you'd put inside a foreach block. Allow me to demonstrate with a simple example...

my @letters = ('a', 'b', 'c'); my @capitals; foreach my $l (@letters) { push @capitals, uc($l); }

Don't do this:

my @letters = ('a', 'b', 'c'); my @capitals = map { $i++; push @capitals, uc($letters[$i]) } @letters +;

Do this:

my @letters = ('a', 'b', 'c'); my @capitals = map { uc($_) } @letters;

What map does basically is to execute the contents of the block on each item in the given list, making a new list from all the results. Note in the above example, the code within the block doesn't need to do any pushing onto @capitals, and it doesn't need to look at @letters - the map function does that for you.

Your particular example can be written as:

my $i = 0; my @names = map { $i++ > 6 ? ($_) : () } @input2arr;

Though using grep in this case might be better.

my $i = 0; my @names = grep { $i++ > 6 } @input2arr;

Or just use an array slice:

my @names = @input2arr[ 7 .. $#input2arr ];
perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

In reply to Re: bit of help with map function by tobyink
in thread bit of help with map function by sweepy838

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.