I'm afraid I'm a bit confused too, in part because I don't see any relation between the code you showed and the issues you describe.

In terms of handling two related arrays, keeping the indexing in sync while being able to skip an iteration when you've seen an array value previously, you could keep everything as-is and add a hash for "seen" values inside the loop:

my %seen = (); for my $i ( 0..$#texts[0] ) { next if ( $seen{$texts[$i] ); $seen{$texts[$i] = 1; ... # do stuff with $texts[$i] and $images[$i] }
But if this loop over the arrays is itself being repeated five times (if I understand you right), then it may be better to structure the data as a hash in the first place, instead of two arrays.

Since the value of @texts appears to work for you as the "key" (you don't want/need to handle a given "text" value more than once), then why not just make this the hash key, and make the value of @images the hash value:

my %text_image; @text_image{@texts} = @images; for my $text ( keys %text_image ) { my $image = $text_image{$text}; # do stuff with $text and $image... }

This assumes that each time you see a given value from @text you should likewise see a repeated value in @image at that index. If a single value of @texts occurs more than once but with different corresponding values of @images, then why do you want to skip repeat occurrences of "text" values? (The above code will only retain the last such pairing, but there are easy ways to fill the hash working from the ends of the arrays to the beginnings.)

Anyway, with that hash approach, the elements of the two arrays remain linked to each other, and it shouldn't matter in what order the hash keys are handed to a for loop over the keys.

(Then again, if for some reason you need the loop to treat the entries in the same specific order as the original arrays, you can build an array of arrays, and use a "%seen" hash when doing that, so as to load only unique values from @text.)


In reply to Re: Keeping separate array data related by graff
in thread Keeping separate array data related by coldfingertips

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.