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:
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.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] }
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |