Update: Many thanks to all for tutoring me on map! I really enjoy learning things quickly from experts.

My favorite solution is Re: Scanning a list for indices - Thanks Fletch++.

What a wonderful language that lets you do things like:

@a1_index{ @a1 } = 0..$#a1;
Folks-

I'm sorry if this has been answered already (I gotta believe it has), but I'm not finding anything with my searches (which I'm admittedly bad at). Any help and all patience is appreciated!

The short version of my question is this. Given 2 lists:

my @a1 = ( qw(one two three four five six seven eight nine ten) ); my @a2 = ( qw(four seven nine) );

How do I generate a third list:

@indices = ( qw( 3 6 8) );
Which contains the indices of where all elements from @a2 show up in @a1? The attached code is my solution, but my bones tell me that there must be a better one. Any help is much appreciated!

-Craig

The long version of my question is that I have an item to add to a listbox that must be kept sorted, and some of the items are highlighted. My current design is to get the current list from the listbox into an array, push the new item, resort, and then slam the entire newly sorted list back into the listbox (I'm open to better options here if this isn't the best).

I also have another list of currently highlighted listbox items (backgrounds are some other color besides the default). As soon as I slam in the newly sorted list, all highlighting in the listbox is (obviously) removed. In order to replace the highlighting I need to find the positional index of each highlighted item in the newly sorted list so I can do...

$listbox->itemconfigure($index, -background=>'highlightyellow');

...since itemconfigure needs the positional index of the thing I want (listbox always frustrates me this way). I suspect a better answer is to use tied variables with the listbox, but I'm having trouble getting my brain around how those work.

use strict; use warnings; use Data::Dumper; my @a1 = ( qw(one two three four five six seven eight nine ten) ); my @a2 = ( qw(four seven nine) ); my @indices; my $c=0; map { foreach my $i (@a2) { if($_ eq $i) {push(@indices, $c)}; } $c++; } @a1; print "indices DUMP:\n", Dumper(\@indices), "\n";

In reply to Scanning a list for indices by cmv

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.