Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

delete(), but for arrays

by vaevictus (Pilgrim)
on May 16, 2002 at 18:50 UTC ( #167074=perlquestion: print w/replies, xml ) Need Help??

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

In perl 5.6.x, there has been a discussion about using delete(), (which was designed to remove key->value pairs from hashes), and having it remove values entirely from arrays in the same fashion, (not just undefing the value for that index, but removing that index entirely).

I understand that in perl 5.005_03, the interpreter will not allow you to use an array in that situation.'

My predicament is this: delete($array[$index]) is more readable than slice($array,$index,1), and more efficient too, I think. What should I do to maintain each of my goals: backward compatibility to 5.005x, readability, and performance?

Replies are listed 'Best First'.
Re: delete(), but for arrays
by demerphq (Chancellor) on May 16, 2002 at 19:09 UTC
    Well, as the following shows delete() does not do anything like splice does on an array. Rather it is exactly the same as undefing the element in question. No reordering of the array is done as would be in splice. (In fact it was the impact of this confusion that lead to the debate about whether it should be allowed or not)
    my @array=(0..9); delete $array[5]; print "'$_'\n" foreach @array; __END__ '0' '1' '2' '3' '4' '' '6' '7' '8' '9'
    HTH

    Yves / DeMerphq
    ---
    Writing a good benchmark isnt as easy as it might look.

Re: delete(), but for arrays
by vladb (Vicar) on May 16, 2002 at 18:55 UTC
    vaevictus askth:

    What should I do to maintain each of my goals: backward compatibility to 5.005x, readability, and performance?

    My answer would be if your sole goal is to assure backward compatibility, then you've really got no other option than stick with coding practices that are accaptable in both newer and older versions of Perl. This means, unfortunately, you'll have to leave slice($array, $index, 1) alone ;).

    If, on the other hand, you are not worried about backward compatibility, I consider it a wholesome pursuit to improve readability and performance of one's code. In this regard, do whichever thou art deems appropriate! Therefore, if you consider delete($ARRAY[2]); as a 'cleaner' alternative to the use of splice(), stick with the former. Indeed, this is a matter of taste and pleasing the audience (whoever will come to look at your code etc.)

    cheers,

    UPDATE: expanded on my initial thought...

    _____________________
    $"=q;grep;;$,=q"grep";for(`find . -name ".saves*~"`){s;$/;;;/(.*-(\d+) +-.*)$/;$_=&#91"ps -e -o pid | "," $2 | "," -v "," "]`@$_`?{print" ++ $1"}:{print"- $1"}&&`rm $1`;print"\n";}
Re: delete(), but for arrays
by kal (Hermit) on May 16, 2002 at 19:25 UTC

    I have a number of similar problems; I was thinking about writing about them in Meditations.

    Essentially, this: the // operator has been deemed such "good stuff" that it's going into 5.8.0, even though it was proposed for 6 (I believe ;). I, too, think it's hot rockin' stuff and want to start using it now, dammit.

    What do I do? For all the machines I maintain, 5.5 is pretty much 'the standard' (although most have 5.6.0). Upgrading isn't too hard I guess.. and luckily, I'm in control of all the machines anyway. But there is a kind of cutting-edge-iness which feels wrong somehow. And especially if I was writing code to be released, having recently experienced the hell that is PHP (some software I wanted to use demanded PHP 4.2.0 minimum, and RedHat haven't even packaged that yet.. dependency hell and then some...), I would want to keep it lowest-common-denominator.

    I think that's my answer. LCD for employment use, LCD for public use, cutting edge anything-goes for me personally ;) And eventually, when the new becomes passe, all my P3rl sk1llz are ready to take full advantage of the new powers bestowed on us :)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2022-05-24 00:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (82 votes). Check out past polls.

    Notices?