Hello Perl Monks...I come today on bended knee humbly begging for any wisdom you can provide...

I am trying to find elements in an array that are positioned 1, 2 and 3 places behind my 'target' element. So if my list is: 1,2,3,4,5,6,7,8,9,8,7,6,5,4,3,2,1, and my 'target' number was 5, I am looking for a way to return elements 6,7,8, and 4,3,2, I need to get the three elements after every time a 5 appears in the list. In actual practice the list will not be sequential and will be somewhat longer, but nowhere near consuming all my memory kinda long, maybe 200-300 items max.

I can get the index of my target number easily enough, however I am wondering if there is a way I can manipulate the index values to 'look ahead'. I've been trying many variations of:

$idxvalue = (pop(@indexes)+1); print "$lines[$indexes[$idxvalue]]\n";

but I am having a hard time finding something that works. It's odd because when I print @indexes I get a list of the index numbers as expected, but then at times I am getting the actual item from the list, rather than the index position number I wanted. Please forgive my ignorance...I am trying my best....which admittedly is not very good :-(

I'm using Strawberry Perl on Windows 10 in case it matters.... Thank you in advance to any who can offer suggestions or advice! TJ.


In reply to Manipulating Array Indexes by TJ777

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.