Need to remove an array from another array

Discussion in 'Perl Misc' started by M.L., Oct 7, 2008.

  1. M.L.

    M.L. Guest

    Given an array: @planets = qw(mercury venus earth mars jupiter saturn uranus
    neptune pluto);
    and @removes = qw(venus mars pluto);

    I'd like to know how to grep the @removes from @planets such that
    @planets = qw(mercury earth jupiter saturn uranus neptune);

    I tried
    @planets = grep (!/$removes[0..$#removes]/, @planets);
    but that didn't work and I'm out of ideas. Any assistance appreciated.
    Thanks.
     
    M.L., Oct 7, 2008
    #1
    1. Advertising

  2. "M.L." <> writes:

    > Given an array: @planets = qw(mercury venus earth mars jupiter saturn uranus
    > neptune pluto);
    > and @removes = qw(venus mars pluto);


    A standard way would be to 'upgrade' the @removes array to ans hash
    and then use the existance of a key in the has as the condition for a
    grep:

    %removes = map { $_ => 1 } @removes;
    @planets = grep { !$remove{$_} } @planets;

    //Makholm
     
    Peter Makholm, Oct 7, 2008
    #2
    1. Advertising

  3. "M.L." <> wrote:
    >Given an array: @planets = qw(mercury venus earth mars jupiter saturn uranus
    >neptune pluto);
    >and @removes = qw(venus mars pluto);


    perldoc -q intersection:
    "How do I compute the difference of two arrays? How do I compute the
    intersection of two arrays?"

    jue
     
    Jürgen Exner, Oct 8, 2008
    #3
  4. M.L.

    Guest

    On Tue, 7 Oct 2008 12:36:41 -0500, "M.L." <> wrote:

    >Given an array: @planets = qw(mercury venus earth mars jupiter saturn uranus
    >neptune pluto);
    >and @removes = qw(venus mars pluto);
    >
    >I'd like to know how to grep the @removes from @planets such that
    >@planets = qw(mercury earth jupiter saturn uranus neptune);
    >
    >I tried
    >@planets = grep (!/$removes[0..$#removes]/, @planets);
    >but that didn't work and I'm out of ideas. Any assistance appreciated.
    >Thanks.
    >
    >

    Why don't you just say it in English, do you really need examples?

    The most important array comparison is thier similarity, not the difference!!!
    Similarity first, difference by default.

    sln
     
    , Oct 8, 2008
    #4
  5. M.L.

    M.L. Guest

    >> Given an array: @planets = qw(mercury venus earth mars jupiter saturn
    >> uranus
    >> neptune pluto);
    >> and @removes = qw(venus mars pluto);

    >
    > A standard way would be to 'upgrade' the @removes array to ans hash
    > and then use the existance of a key in the has as the condition for a
    > grep:
    >
    > %removes = map { $_ => 1 } @removes;
    > @planets = grep { !$remove{$_} } @planets;


    I found your solution to be not only elegant, but the easiest to understand.
    After making a slight correction ($remove{$_} -> $removes{$_} ), your
    solution worked like a charm. Thank you so much.
     
    M.L., Oct 8, 2008
    #5
  6. M.L.

    M.L. Guest

    >>Given an array: @planets = qw(mercury venus earth mars jupiter saturn
    >>uranus
    >>neptune pluto);
    >>and @removes = qw(venus mars pluto);

    >
    > perldoc -q intersection:
    > "How do I compute the difference of two arrays? How do I compute the
    > intersection of two arrays?"


    Thank you for your perldoc reference. While it provided a little more
    horsepower than I needed, I found it very enlightening. I'll keep it in my
    Perl notes file.
     
    M.L., Oct 8, 2008
    #6
  7. "M.L." <> wrote:
    >Given an array: @planets = qw(mercury venus earth mars jupiter saturn uranus
    >neptune pluto);
    >and @removes = qw(venus mars pluto);
    >
    >I'd like to know how to grep the @removes from @planets such that
    >@planets = qw(mercury earth jupiter saturn uranus neptune);


    Coming to think of it, your spec via example is rather incomplete:
    - what about if @planets contains multiple entries with the same value,
    e.g. 'mars'? Are you supposed to remove all occurences of 'mars' from
    @planets, even if @removes contains only one entry 'mars'? And if only
    one is to be removed, then which one? Any? The first?
    -probably less critical: what about if @removes contain an element that
    is not part of @planets? Error? Or just ignore?

    It appears to me you are using the arrays to represent mathematical
    sets: elements are unique and sequence doesn't matter. If this is true,
    then a hash would be a much better data structure.

    jue
     
    Jürgen Exner, Oct 8, 2008
    #7
  8. M.L.

    Guest

    On Wed, 8 Oct 2008 00:16:38 -0500, "M.L." <> wrote:

    >>> Given an array: @planets = qw(mercury venus earth mars jupiter saturn
    >>> uranus
    >>> neptune pluto);
    >>> and @removes = qw(venus mars pluto);

    >>
    >> A standard way would be to 'upgrade' the @removes array to ans hash
    >> and then use the existance of a key in the has as the condition for a
    >> grep:
    >>
    >> %removes = map { $_ => 1 } @removes;
    >> @planets = grep { !$remove{$_} } @planets;

    >
    >I found your solution to be not only elegant, but the easiest to understand.
    >After making a slight correction ($remove{$_} -> $removes{$_} ), your
    >solution worked like a charm. Thank you so much.


    It just makes me sick listening to crap like this. I wan't to puke in your face.

    sln
     
    , Oct 8, 2008
    #8
  9. M.L.

    M.L. Guest

    >>Given an array: @planets = qw(mercury venus earth mars jupiter saturn
    >>uranus
    >>neptune pluto);
    >>and @removes = qw(venus mars pluto);
    >>
    >>I'd like to know how to grep the @removes from @planets such that
    >>@planets = qw(mercury earth jupiter saturn uranus neptune);

    >
    > Coming to think of it, your spec via example is rather incomplete:
    > - what about if @planets contains multiple entries with the same value,
    > e.g. 'mars'? Are you supposed to remove all occurences of 'mars' from
    > @planets, even if @removes contains only one entry 'mars'? And if only
    > one is to be removed, then which one? Any? The first?
    > -probably less critical: what about if @removes contain an element that
    > is not part of @planets? Error? Or just ignore?
    >
    > It appears to me you are using the arrays to represent mathematical
    > sets: elements are unique and sequence doesn't matter. If this is true,
    > then a hash would be a much better data structure.


    Nothing really complex about my setup. I created @planets as an easy to
    understand analogue representing the unique form variables on my HTML form.
    I deliberately created the @removes array of variables as a means to get rid
    of any unneeded form variables before they were printed out for an email
    response.

    For my email response problem it would be necessary to get rid of *any*
    duplicate entries in @planets that were specified in @removes since I
    wouldn't want the recipient to ever see them. Since I created @removes for a
    specific duty, I would never allow it to have duplicate entries. Even if it
    did, I've already used Google to learn how to get rid of duplicate entries
    from an array. So I'm all set for now.
     
    M.L., Oct 8, 2008
    #9
    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. Simon-Pierre  Jarry
    Replies:
    2
    Views:
    2,413
    Henrik
    Aug 10, 2005
  2. Yourself

    Remove control from another control

    Yourself, Mar 5, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    2,300
    Yourself
    Mar 5, 2006
  3. tshad
    Replies:
    6
    Views:
    21,537
    tshad
    Aug 8, 2006
  4. Andrew Jocelyn
    Replies:
    2
    Views:
    1,562
    Allen Chen [MSFT]
    Apr 27, 2009
  5. Bryan
    Replies:
    8
    Views:
    155
    Brian McCauley
    May 7, 2004
Loading...

Share This Page