[Perl] suppression d'une valeu d'un hash

Malco -  
jisisv Messages postés 3678 Statut Modérateur -
Bonjour,

En perl, est il possible de supprimer la valeur d'une ligne d'un hash ?

exemple :
j'ai ce hash:
%hash = (
"key1" => "valeur1",
"key2" => "valeur2",
);

et je veut me retrouver avec un hash comme celui ci :
%hash = (
"key2" => "valeur2",
);

c'est possible ? si oui, comment faire ?

Merci à tous de votre aide.

1 réponse

  1. jisisv Messages postés 3678 Statut Modérateur 936
     
    Hello,

    extrait de perldoc perlfunc

    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).

    Returns each element so deleted or the undefined
    value if there was no such element. Deleting from
    $ENV{} modifies the environment. Deleting from a
    hash tied to a DBM file deletes the entry from the
    DBM file. Deleting from a "tie"d hash or array
    may not necessarily return anything.

    Deleting an array element effectively returns that
    position of the array to its initial, uninitial-
    ized 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".
    .....
    #! /usr/bin/perl -w
    %hash = ("key0" => "val0", "key1" => "val1", "key2" => "val2");
    delete($hash{"key1"});

    devrait faire l'affaire

    Johan
    The software said "Requires Windows98, Win2000, or better,
    So I installed Unix.
    1