splicing two arrays

Discussion in 'Perl Misc' started by Matija Papec, Jul 31, 2003.

  1. Matija Papec

    Matija Papec Guest

    Here is the problem which IMHO isn't trivialy(in one line) solvable in
    perl5; I have two arrays of same length and want to remove elements from
    them depending on %h(all values are just for illustration, there is no
    relation between them),

    my @arr1 = 30..60;
    my @arr2 = 10..40;

    my %h = (
    33 => 'value',
    43 => 'value',
    45 => 'value',
    51 => 'value',
    53 => 'value',
    );


    Now, simple grep does a right thing,
    @arr1 = grep exists $h{$_}, @arr1;

    but it leaves me with @arr2 where corresponding elements to @arr1 should
    also be removed(when removing n-th from @arr1, remove n-th from @arr2 too).

    I ended up with simultaneous array rotation and I'm curios now if there is a
    better way? :)



    --
    Matija
     
    Matija Papec, Jul 31, 2003
    #1
    1. Advertising

  2. Matija Papec

    Uri Guttman Guest

    >>>>> "MP" == Matija Papec <> writes:

    MP> my @arr1 = 30..60;
    MP> my @arr2 = 10..40;

    MP> my %h = (
    MP> 33 => 'value',
    MP> 43 => 'value',
    MP> 45 => 'value',
    MP> 51 => 'value',
    MP> 53 => 'value',
    MP> );


    MP> Now, simple grep does a right thing,
    MP> @arr1 = grep exists $h{$_}, @arr1;

    MP> but it leaves me with @arr2 where corresponding elements to @arr1
    MP> should also be removed(when removing n-th from @arr1, remove n-th
    MP> from @arr2 too).

    grep on the indexes and not on the values. then slice both arrays.

    my @indexes = grep $h{$arr1[$_]}, @arr1 ;

    @arr1 = @arr1[@indexes] ;
    @arr2 = @arr2[@indexes] ;

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, Jul 31, 2003
    #2
    1. Advertising

  3. Uri Guttman <> wrote:

    >>>>>> "MP" == Matija Papec <> writes:

    >
    > MP> my @arr1 = 30..60;
    > MP> my @arr2 = 10..40;
    >
    > MP> my %h = (
    > MP> 33 => 'value',
    > MP> 43 => 'value',
    > MP> 45 => 'value',
    > MP> 51 => 'value',
    > MP> 53 => 'value',
    > MP> );


    > grep on the indexes and not on the values. then slice both arrays.
    >
    > my @indexes = grep $h{$arr1[$_]}, @arr1 ;


    You mean like this? :)

    my @indexes = grep $h{$arr1[$_]}, 0..$#arr1 ;



    > @arr1 = @arr1[@indexes] ;
    > @arr2 = @arr2[@indexes] ;
     
    David K. Wall, Jul 31, 2003
    #3
  4. Matija Papec

    Uri Guttman Guest

    >>>>> "DKW" == David K Wall <> writes:

    DKW> Uri Guttman <> wrote:
    >>>>>>> "MP" == Matija Papec <> writes:

    >>

    MP> my @arr1 = 30..60;
    MP> my @arr2 = 10..40;
    >>

    MP> my %h = (
    MP> 33 => 'value',
    MP> 43 => 'value',
    MP> 45 => 'value',
    MP> 51 => 'value',
    MP> 53 => 'value',
    MP> );

    >> grep on the indexes and not on the values. then slice both arrays.
    >>
    >> my @indexes = grep $h{$arr1[$_]}, @arr1 ;


    DKW> You mean like this? :)

    DKW> my @indexes = grep $h{$arr1[$_]}, 0..$#arr1 ;

    yeah. too quick on the reply.

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, Jul 31, 2003
    #4
  5. Matija Papec

    matija Guest

    On Thu, 31 Jul 2003 18:14:14 GMT, Uri Guttman <> wrote:
    > MP> Now, simple grep does a right thing,
    > MP> @arr1 = grep exists $h{$_}, @arr1;
    >
    > MP> but it leaves me with @arr2 where corresponding elements to @arr1
    > MP> should also be removed(when removing n-th from @arr1, remove n-th
    > MP> from @arr2 too).
    >
    >grep on the indexes and not on the values. then slice both arrays.
    >
    >my @indexes = grep $h{$arr1[$_]}, @arr1 ;
    >
    >@arr1 = @arr1[@indexes] ;
    >@arr2 = @arr2[@indexes] ;


    Tnx, it appears that perl6 will handle this in its own way.
     
    matija, Aug 1, 2003
    #5
  6. Matija Papec

    matija Guest

    On Thu, 31 Jul 2003 18:34:36 GMT, Steve Grazzini <>
    wrote:

    >> I ended up with simultaneous array rotation and I'm curios
    >> now if there is a better way? :)

    >
    >How about:
    >
    > for my $i (0..$#arr1) {
    > next unless exists $h{$arr1[$i]};
    > splice @arr1, $i, 1;
    > splice @arr2, $i, 1;
    > }


    Unfortunately this doesn't work as expected as arrays continuously
    shrink so when $i reaches initial $#arr1, real @arr1 is far smaller.

    >Or is that the "simultaneous rotation"?


    Nope, something like
    push @arr1, (shift @arr1);
    push @arr2, (shift @arr2);
     
    matija, Aug 1, 2003
    #6
  7. Matija Papec

    Greg Bacon Guest

    In article <>,
    matija <> wrote:

    : On Thu, 31 Jul 2003 18:34:28 -0000, (Greg Bacon)
    : wrote:
    :
    : [...]
    :
    : >Oh, that's a different problem. Is there a reason you're using
    : >parallel arrays rather than a list of lists?
    :
    : I have no real reason except I started coding in such way with two
    : arrays (@arr1 has cgi-form names and @arr2 corresponding sql column
    : names).

    When I find myself starting to write parallel arrays, I almost always
    combine them to make arrays of arrays. See the perllol manpage for the
    mechanics. Doing it that way makes eliminating corresponding elements
    trivial.

    Greg
    --
    The right most valued by all civilized men is the right to be left alone.
    -- Justice Louis Brandeis
     
    Greg Bacon, Aug 1, 2003
    #7
  8. matija <> wrote:
    > Steve Grazzini <> wrote:
    > >> I ended up with simultaneous array rotation and I'm curios
    > >> now if there is a better way? :)

    > >
    > >How about:
    > >
    > > for my $i (0..$#arr1) {
    > > next unless exists $h{$arr1[$i]};
    > > splice @arr1, $i, 1;
    > > splice @arr2, $i, 1;
    > > }

    >
    > Unfortunately this doesn't work as expected as arrays
    > continuously shrink so when $i reaches initial $#arr1,
    > real @arr1 is far smaller.


    Yeah... I read your logic backwards.

    for my $i (0..$#arr1) {
    next if exists $h{$arr1[$i]}; # KEEP them
    splice @arr1, $i, 1;
    splice @arr2, $i, 1;
    }

    --
    Steve
     
    Steve Grazzini, Aug 2, 2003
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Anunay

    Object Splicing

    Anunay, May 17, 2006, in forum: C++
    Replies:
    5
    Views:
    628
    Joe Van Dyk
    May 22, 2006
  2. Paul Rubin

    proposed PEP: iterator splicing

    Paul Rubin, Apr 15, 2007, in forum: Python
    Replies:
    8
    Views:
    278
    Georg Brandl
    Apr 15, 2007
  3. Kreso

    Subclassing list and splicing

    Kreso, Sep 3, 2009, in forum: Python
    Replies:
    3
    Views:
    377
    Nobody
    Sep 4, 2009
  4. Kev Jackson
    Replies:
    2
    Views:
    114
  5. Csaba  Gabor

    Splicing exercise

    Csaba Gabor, Oct 19, 2009, in forum: Javascript
    Replies:
    32
    Views:
    271
    Garrett Smith
    Oct 22, 2009
Loading...

Share This Page