My favorite solution is Re: Scanning a list for indices - Thanks Fletch++.
What a wonderful language that lets you do things like:
Folks-@a1_index{ @a1 } = 0..$#a1;
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:
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!@indices = ( qw( 3 6 8) );
-Craig
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |