Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

complementary array indices

by coldy (Scribe)
on May 11, 2009 at 00:43 UTC ( [id://763169]=perlquestion: print w/replies, xml ) Need Help??

coldy has asked for the wisdom of the Perl Monks concerning the following question:

I have an array @a that gives some indices of another array @b

eg , @a_indices = (1,3,5)

If I know the size of @b, is there an easy way to determine the complementary positions?

eg if $b = 10 what are the positions that are not @a.

I was thinking I could create an array of indices

@b_indices = (0..9)
and then find the non-intersecting elements with @a_indices. Surely there must be an easier way? Thanks.

Replies are listed 'Best First'.
Re: complementary array indices
by almut (Canon) on May 11, 2009 at 01:04 UTC
    Surely there must be an easier way?

    Why? Looks pretty easy. For example:

    my @a_indices = (1,3,5); my @b_indices = (0..9); @b_indices[@a_indices] = (); @b_indices = grep defined, @b_indices; use Data::Dumper; print Dumper \@b_indices; __END__ $VAR1 = [ 0, 2, 4, 6, 7, 8, 9 ];
Re: complementary array indices
by GrandFather (Saint) on May 11, 2009 at 01:33 UTC

    How big are your arrays? An CPU inefficient but memory efficient version is:

    my @a_indices = (1,3,5); my @b = map {my $t = $_; (grep {$t == $_} @a_indices) ? undef : $t} 0 +.. 9; print join ', ', grep {defined} @b;

    Prints:

    0, 2, 4, 6, 7, 8, 9

    On the other hand:

    my @a_indices = (1,3,5); my %hasIndex = map {$_ => undef} @a_indices; my @b = map {exists $hasIndex{$_} ? undef : $_} 0 .. 9;

    is likely to be faster for large arrays, but at the expense of creating a hash.


    True laziness is hard work
      A faster way to produce %hasIndex is:
      my %hasIndex; undef(@hasIndex{@a_indices});
      However I wouldn't recommend using that trick in production code.
Re: complementary array indices
by ig (Vicar) on May 11, 2009 at 05:31 UTC
Re: complementary array indices
by Anonymous Monk on May 11, 2009 at 00:55 UTC
    Can you define your use of complementary?
Re: complementary array indices
by Marshall (Canon) on May 11, 2009 at 09:08 UTC
    I don't think that you have specified the problem.
    Please show data set A and data set B and the resulting data set C.

Re: complementary array indices (5.10)
by lodin (Hermit) on May 12, 2009 at 00:07 UTC

    If you have Perl 5.10 you can do

    use 5.010; my @a = (1, 3, 5); my @b = qw/ 0 x 2 x 4 x 6 7 8 9 /; print @b[grep { not $_ ~~ @a } 0 .. $#b]; __END__ 0246789

    lodin

Re: complementary array indices
by rir (Vicar) on May 12, 2009 at 17:30 UTC
    When I was doing this, the @indices array was presorted.
    my @arry = qw{ 10 11 12 13 14 15 16 17 18 }; my @indices = qw{ 3 5 1 }; my @remainder = ( 0 .. $#arry ); splice @remainder, $_, 1 for reverse sort { $a <=> $b }@indices; $,=","; print @remainder, $/; __DATA__ 0,2,4,6,7,8,
    Be well,
    rir

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://763169]
Approved by jettero
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2024-04-20 13:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found