Each pass through saves the position of each match to a string into an array (arrayB). This array of matches to the first string becomes the first element of arrayA.

You need two values here, either position + length or start-positon and end-position of each match. Perl provides the variables @- and @+ for the latter. An example is in perlretut.

You could store these values in a single string, e.g. "16-25", or store them in an array. To push that array into arrayB, the array's representation has to be a scalar, because there can only be one element in each slot of an array. So, you need an anonymous array which can be accessed via a reference (see e.g. perlreftut).

my @arrayC; while(defined($textblock) = <>) { my @arrayA = (); foreach my $string (@strings) { my @arrayB = (); while($textblock =~ /$string/) { push @arrayB, [ $-[0], $+[0] ]; } push @arrayA, \@arrayB; } push @arrayC, \@arrayA; }

Note that we take references to the lexically scoped arrays @arrayA and @arrayB (with the "\" operator, see perlop), which are re-initialized at each pass through their loop block.

Ideally, I would then format each text block to highlight each string-match in a particular color, based on the position of hits stored in each ArrayB, corresponding to the particular string matched stored in each ArrayA, corresponding to the text block the hits are found in (the position in ArrayC.

So you need to save your textblocks somewhere to do the formatting, if you don't want to read the file twice. You could store it together with the correspondig matches you've found in @arrayC (instead of storing the matches only):

push @arrayC, [ $textblock, \@arrayA ];

-shmem

_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                              /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

In reply to Re: appending and array into last element of an array? by shmem
in thread appending and array into last element of an array? by mdunnbass

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.