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 7600567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:56:29+00:00 2026-05-30T22:56:29+00:00

I need a subroutine that completely removes an array element in place . The

  • 0

I need a subroutine that completely removes an array element in place. The following code fails:

sub del
{
    splice(@_,2,1);
}

@array=(0..5);
print "@array"."\n";
del(@array);
print "@array"."\n";

The same array is printed again, i.e. the element has not been removed.
However, if I use the splice() in the main body of the program instead of calling a subroutine, it works.

  • 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-05-30T22:56:30+00:00Added an answer on May 30, 2026 at 10:56 pm

    While the scalar elements of @_ are aliased to the data which is passed in, @_ itself is a different variable. This means $_[1] = "foo" will alter $_[1] but push @_, "foo" will not alter @_. Otherwise my $self = shift would be a Bad Thing.

    You need to pass in the array as a reference.

    sub del {
        my $array_ref = shift;
    
        splice @$array_ref, 2, 1;
    
        return;
    }
    
    del \@array;
    

    If you absolutely must keep the del @array interface, this is one of the few places where it’s appropriate to use a prototype.

    sub del(\@) {
        my $array_ref = shift;
    
        splice @$array_ref, 2, 1;
    
        return;
    }
    
    del @array;
    

    The \@ prototype tells Perl to pass in @array by reference. I would recommend against doing this for two reasons. First, prototypes have a pile of caveats which make them not worth the trouble.

    More importantly, it makes it non-obvious that del will modify its arguments. Normally user defined Perl functions copy their arguments, so you can look at foo @array and be reasonably sure @array will not be altered by foo. This enables one to skim the code quickly for things which will affect a variable. A reference prototype throws this out the window. Now every function must be examined for a possible hidden pass-by-reference.

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

Sidebar

Related Questions

In a shell-script (or Perl) I need a subroutine that returns for all my
I have about 100 sub routines that I need to use..I am going to
I have a variable that I need to pass to a subroutine. It is
Need a function that takes a character as a parameter and returns true if
Need to an expression that returns only things with an I followed by either
Need to locate the following pattern: The letter I followed by a space then
Need a function like: function isGoogleURL(url) { ... } that returns true iff URL
I need to run a VBA subroutine 4 seconds later. The subroutine takes one
I have a subroutine called debug I use in my code. It basically allows
I have a mission critical Perl-CGI server-side application that I need to extend or

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.