Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Array question

by Theseus (Pilgrim)
on Jul 10, 2002 at 16:31 UTC ( [id://180786]=perlquestion: print w/replies, xml ) Need Help??

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

Let's say I have the following:
my @array = ("index0","index1","index2","index3","index4"); delete $array[2]; # @array is now ("index0","index1",undef,"index3","index4") # but what I wanted was: # @array = ("index0","index1","index3","index4");
I know I could create a subroutine that sorted the array and then shifted the undefs off, but I need to be able to do this without altering the order of the array(besides eliminating undefs and collapsing the array to fill in the empty space). Any help would be appreciated, thanks.

Replies are listed 'Best First'.
(zdog) Re: Array question
by zdog (Priest) on Jul 10, 2002 at 16:34 UTC
    Take a look at splice. In your case, you can do:

    splice (@array, 2, 1);

    Zenon Zabinski | zdog | zdog@perlmonk.org

Re: Array question
by Abigail-II (Bishop) on Jul 10, 2002 at 16:35 UTC
    You only want to use delete on an array in special circumstances. What you want is splice.
    my @array = ("index0","index1","index2","index3","index4"); splice @array => 2, 1; # Replace a 1 element subarray starting # at index 2 with an empty list. # Array is now ("index0", "index1","index3","index4");

    Abigail

Re: Array question
by sschneid (Deacon) on Jul 10, 2002 at 16:36 UTC
    Consider splice...
    my @array = (1, 2, 3, 4, 5, 6); splice @array, -3, 1; # @array is now (1, 2, 3, 5, 6). splice @array, 2, 1; # ...and now it's (1, 2, 5, 6).
    scott.
Re: Array question
by bronto (Priest) on Jul 10, 2002 at 17:05 UTC

    I won't repeat "use splice", :-), but I just would like to show you some spots of perldoc where that behaviour is described explicitly:

    delete EXPR
    Given an expression that specifies a hash element, array element, hash slice, or array slice, deletes the specified element(s) from the hash or array. In the case of an array, if the array elements happen to be at the end, the size of the array will shrink to the highest element that tests true for exists() (or 0 if no such element exists).
    ...
    Deleting an array element effectively returns that position of the array to its initial, uninitialized state. Subsequently testing for the same element with exists() will return false. Note that deleting array elements in the middle of an array will not shift the index of the ones after them down--use splice() for that. See "exists".

    Is the above a subtle RTFM? No, it's not, don't worry. It's just a I lost one hour on my code while I could lose just 5 minutes on the docs ;-)

    Ciao!
    --bronto

    # Another Perl edition of a song:
    # The End, by The Beatles
    END {
      $you->take($love) eq $you->made($love) ;
    }

      Believe it or not, I did view that very same manpage, and the splice() comment just completely got past me somehow. That's what happens when I stay at work late trying to find the solution to something that's been bugging me... *sigh*

        Ahah! :-) No problem brother, I know what you mean!

        Ciao!
        --bronto

        # Another Perl edition of a song:
        # The End, by The Beatles
        END {
          $you->take($love) eq $you->made($love) ;
        }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-04-25 09:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found