Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8028833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T00:14:45+00:00 2026-06-05T00:14:45+00:00

How do I use grep or map to delete elements from an array or

  • 0

How do I use grep or map to delete elements from an array or reference to array? I’m having problems using splice to remove one or more elements from a reference to array and would like to see whether grep or map can offer me a better solution.

@removedElements = grep /test/, @array;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T00:14:47+00:00Added an answer on June 5, 2026 at 12:14 am

    You are saying "array or reference array" like it was two different things. Which is confusing.

    I assume that since you have named your array @removedElements, what you are trying to ask is how to remove the elements from @array and put them in @removedElements.

    @removedElements = grep /test/, @array;   
    @array           = grep ! /test/, @array;
    

    A simple negation of the test will yield either list. You can also do a loop:

    my (@removedElements, @rest);
    for (@array) {
        if (/test/) {
            push @removedElements, $_;
        } else {
            push @rest, $_;
        }
    }
    

    Which has the benefit of fewer checks being performed.

    In order to use splice, you would need to keep track of indexes, and I’m not sure its worth it in this case. It certainly would not make your code easier to read. Similarly, I doubt map would be much more useful than a regular loop.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble with grep.. Which four patterns should I use with PHP's preg_grep
How can I use grep to select lines from line1 to line2 in some
How do I use grep() to get NAs from a vector? i.e: when I
How can I use grep to search only one column within a CSV file,
We can use grep to return the lines with matched words, but how to
On Linux, I often use grep with context to find usage information in man
I'm trying to use ack-grep as a replacement for grep + find in Emacs
I usually use the following pipeline to grep for a particular search string and
Background: sometimes I use ps aux | grep process_name to figure out some statistics
I code in vim. I use git; and love git grep. Does anyone have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.