Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

how to delete without resulting in undef

by jithoosin (Scribe)
on Nov 17, 2005 at 06:11 UTC ( [id://509302]=perlquestion: print w/replies, xml ) Need Help??

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

Hi monks
I have an array which is an element of a hash.But my aim to use delete an array element with out causing an "undef" to be element of that array
  • Comment on how to delete without resulting in undef

Replies are listed 'Best First'.
Re: how to delete without resulting in undef
by BrowserUk (Patriarch) on Nov 17, 2005 at 06:27 UTC

    You probably want to use splice which removes elements entirely from the array, rather than delete which only deletes their values:

    perl> @a = (1 .. 10);; perl> delete $a[ 3 ];; perl> print "@a";; Use of uninitialized value in join or string at 1 2 3 5 6 7 8 9 10 perl> @a = (1 .. 10);; perl> splice @a, 3, 1;; perl> print "@a";; 1 2 3 5 6 7 8 9 10

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: how to delete without resulting in undef
by Hena (Friar) on Nov 17, 2005 at 06:24 UTC
    Command splice is one. It removes the elements completely.

    $array[$n]="" clears it.
Re: how to delete without resulting in undef
by blazar (Canon) on Nov 17, 2005 at 13:07 UTC

    As others already pointed out splice is the way to go, since delete only shrinks the array if the element you're deleting is at the end of the array. But the former is more costly. So, depending on you actual code/application, suppose that you may have to eliminate a few elements at a time: you may use the latter and "empty the trash can" later:

    delete @array[bad_indices()]; @array = grep defined, @array;

    Of course this assumes that you do not want want undefined elements in your array, which seems to be the case, from the description you give.

Re: how to delete without resulting in undef
by jbrugger (Parson) on Nov 17, 2005 at 06:42 UTC
    However if you WANT to use 'delete', you allways can do it like this:
    #!/usr/bin/perl -w use strict; # updated: Assuming you use strict and warnings :) my @a = (1..10); undef $a[3]; foreach (@a) { print $_ if defined($_); }
    "We all agree on the necessity of compromise. We just can't agree on when it's necessary to compromise." - Larry Wall.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://509302]
Approved by jbrugger
Front-paged by Roy Johnson
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2024-04-19 01:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found