LogIn E-mail
¼³°èÀ̾߱â
Deleting from a Hash , defined , exists
# 113 JMJS    25.7.9 10:40

if (defined $hash{$key}) { }

if (exists $hash{$key}) { }

my %hash = (a => 1, b => 0, c => undef, e=>"");

# key  value  defined  exists
a          1        1       1
b          0        1       1
c      undef        0       1
d      undef        0       0
e      undef        1       1



https://www.oreilly.com/library/view/perl-cookbook/1565922433/ch05s04.html

You want to remove an entry from a hash so that it doesn¡¯t show up with `keys`, `values`, or `each`. If you were using a hash to associate salaries with employees, and an employee resigned, you¡¯d want to remove their entry from the hash.

Use the `delete` function:

```
# remove $KEY and its value from %HASH
delete($HASH{$KEY});
```

Sometimes people mistakenly try to use `undef` to remove an entry from a hash. `undef` `$hash{$key}` and `$hash{$key}` `=` `undef` both make `%hash` have an entry with key `$key` and value `undef`.

The `delete` function is the only way to remove a specific entry from a hash. Once you¡¯ve deleted a key, it no longer shows up in a `keys` list or an `each` iteration, and `exists` will return false for that key.

This demonstrates the difference between `undef` and `delete`:

```
# %food_color as per Introduction
sub print_foods {
    my @foods = keys %food_color;
    my $food;

    print "Keys: @foods\n";
    print "Values: ";

    foreach $food (@foods) {
        my $color = $food_color{$food};

        if (defined $color) {
            print "$color ";
        } else {
            print "(undef) ";
        }
    }
    print "\n";
}

print "Initially:\n";
print_foods();

print "\nWith Banana undef\n";
undef $food_color{"Banana"};
print_foods();

print "\nWith Banana deleted\n";
delete $food_color{"Banana"};
print_foods();

Initially:

Keys: Banana Apple Carrot Lemon

Values: yellow red orange yellow

With Banana undef

Keys: Banana Apple Carrot Lemon

Values: (undef) red orange yellow

With Banana deleted

Keys: Apple Carrot Lemon

Values: red orange yellow ...
```

Get  now with the O¡¯Reilly learning platform.

O¡¯Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.

°Ô½Ã¹°: 115 °Ç, ÇöÀç: 1 / 1 ÂÊ
¹øÈ£ Á¦       ¸ñ ÀÛ¼ºÀÚ µî·ÏÀÏ ¹æ¹®
115  perl format JMJS 24.6.22 496
114  perl -p -i -e s/abc/def/g JMJS 24.3.26 420
113  Deleting from a Hash , defined , exists JMJS 25.7.9 501
112  yaml file Àбâ JMJS 23.11.30 545
111  alzipcon JMJS 22.11.26 596
110  file2date.pl JMJS 24.6.1 698
109  UTF-8, EUC-KR JMJS 19.12.15 718
108  wget.pl LWP Simple get JMJS 19.9.17 723
107  X JMJS 19.9.17 690
106  X JMJS 19.9.17 706
105  X JMJS 19.9.17 713
104  X JMJS 19.9.17 705
103  X JMJS 19.9.17 703
102  X JMJS 19.9.17 681
101  gnome-terminal JMJS 25.7.3 705
100  jsame.pl JMJS 09.5.21 2277
99  jos.pl JMJS 08.11.29 1939
98  jmngr.pl - jmjspro manager JMJS 08.11.29 1994
97  vhdl.pm JMJS 08.11.29 1884
96  jreg.pl JMJS 08.11.29 1943
95  jtree.pm JMJS 08.11.29 1929
94  w2c.pl JMJS 08.11.29 1992
93  jdread.pl JMJS 08.11.29 1731
92  ffile.zip ffilediff.pl JMJS 09.4.24 1783
91  rgb2ycbcr.pl JMJS 10.5.12 2081
90  proview.pl JMJS 08.11.29 2136
89  smi_shift.pl JMJS 10.10.3 1790
88  srt2smi.pl srtshift.pl JMJS 18.7.14 1989
87  Perl CGI Input Test, getenv.cgi JMJS 18.11.21 1634
86  hash JMJS 24.6.22 1924
85  tr a-z A-Z ġȯ , ´ë¼Ò¹®ÀÚ º¯È¯ , ¹®ÀÚ¿­ Á¶°Ç , ... JMJS 24.6.22 1618
84  JARRAY->jfunc JMJS 08.10.2 1689
83  ¼öÄ¡ÇÔ¼ö - ... JMJS 09.5.7 2158
82  ¿ªÇà·Ä3x3 JMJS 09.5.7 2316
81  hex2int.pl JMJS 10.9.12 2847
80  stat - file status º¸±â JMJS 07.12.16 1822
79  Perl Tip JMJS 24.6.22 2880
78  SMI file sync delay JMJS 07.7.18 1880
77  rmword.pl JMJS 07.6.18 1820
76  pack unpack JMJS 24.6.22 1864
75  [Perl]read_dir.pl JMJS 07.2.22 2115
74  [Perl]jtab2char - tab¹®ÀÚ¸¦ ƯÁ¤ char·Î ¹Ù²ãÁÖ±â JMJS 07.2.22 2013
73  Perl ¼ýÀÚ Sorting JMJS 11.5.17 2048
72  [CGI]GET and POST JMJS 06.11.20 3641
71  Perl Grammer JMJS 06.9.4 3232
70  Ç¥ÁØ ¸ðµâ JMJS 06.8.10 2199
69  Getopt::Long example JMJS 06.8.10 2283
68  array of array JMJS 06.4.22 1803
67  [HTML]Å×ÀÌºí ¹è°æ¿¡ ±×¸²³Ö±â JMJS 06.3.8 1896
66  l2w.pm - line to word JMJS 08.12.1 1907
65  make_random_chars JMJS 05.8.10 1709
64  mail º¸³»´Â script JMJS 05.3.23 1706
63  ½ÇÇàÆÄÀÏÀ» ½ÇÇà½Ã۰í Ç¥ÁØ¿¡·¯ Àбâ JMJS 09.2.25 1729
62  ÆË¾÷â(°øÁöâ)¶ç¿ì±â Jinsung 03.12.16 2734
61  print <<ENDLINE JMJS 03.12.5 1771
60  [HTML]Target JMJS 03.5.21 1844
59  [Perl]jutil.pm JMJS 03.5.3 1987
58  [Perl]jdiff.pl JMJS 02.6.6 1996
57  [Perl]cfilter.pm - commant filter JMJS 02.1.31 1984
56  [HTML]ÀÚµ¿À¸·Î ´Ù¸¥ ÆäÀÌÁö À̵¿ JMJS 02.1.19 1834
55  STDIN file open glob -w warning JMJS 24.6.22 1897
54  opendir , readdir , closedir JMJS 24.6.22 1862
53  rand_diff JMJS 01.10.23 1793
52  vcd2v.pl JMJS 15.8.25 1747
51  file upload JMJS 24.6.22 1784
50  umask(070) <- chmod o-rwx Áø¼º 01.3.16 1969
49  head2index Áø¼º 01.3.15 1649
48  grep.pl JMJS 11.1.20 1784
47  chr(13) = \M Áø¼º 01.3.15 1629
46  stty JMJS 01.3.12 1743
45  des.pl JMJS 01.3.8 1638
44  chr_plus.pl JMJS 01.3.8 1697
43  get passwd in unix JMJS 19.8.18 1606
42  unsort JMJS 19.9.10 1937
41  read from website web JMJS 19.5.15 1577
40  vec JMJS 01.3.8 1891
39  chomp $_ @_ say defined use 5.010 state grep any ... JMJS 18.10.27 1717
38  [Perl Tk] µµ½ºÃ¢ ¾øÀÌ, ÆÞ Tk ½ºÅ©¸³Æ® ½ÇÇàÇϱâ; ... JMJS 17.9.30 1758
37  @bs = sort {$a <=> $b} @as; JMJS 17.7.12 1824
36  if ( -e -z -s -f -d -T -B -M ) JMJS 16.3.10 1858
35  mkmm.pl - make freemind JMJS 12.5.22 1831
34  perl -pi.orig -e "s/\bu_(\w+_t)\b/u$1/g" *.c JMJS 11.8.26 1838
33  .xls output csv ÆÄÀÏ¿¡ \n "°¡ ÀÖÀ»¶§ JMJS 11.4.13 1755
32  jdcat.pl juncat.pl JMJS 11.2.24 1893
31  print chr(ord('a')); JMJS 01.3.8 1797
30  require "/user/home/jin1/jin1.pm"; JMJS 00.11.28 1722
29  for(my $i=0; $i < @abc; $i ++) {} JMJS 00.11.15 1742
28  remove blank $abc =~ s/[\s]+//g; Á¤±ÔÇ¥Çö½Ä ... JMJS 25.7.16 1710
27  array and for JMJS 00.11.14 1583
26  È¨ÆäÀÌÁö ÀÚµ¿ À̵¿ JMJS 00.10.28 1684
25  html¿¡¼­ ¾²ÀÌ´Â ±âȣǥ½Ã¹æ¹ý JMJS 00.9.30 1519
24  ¹è¿­ÀÇ ¹üÀ§ ¿¬»êÀÚ JMJS 00.9.20 1927
23  for(1..3) { print "$_";} JMJS 09.4.24 1577
22  package JMJS 24.6.22 1646
21  ÁÙ¿¡¼­ ÇѱÛÀÚ °íÄ¡±â JMJS 00.8.7 1692
20  hotkey function by getc JMJS 00.7.21 1715
19  read, sysread, syswrite JMJS 00.7.21 2670
18  crypt JMJS 00.7.13 1959
17  csv2gan.pl JMJS 11.4.4 1947
16  hexa JMJS 24.6.18 1744
15  file in out.pl JMJS 00.7.5 1912
14  ARGV JMJS 00.7.5 1799
13  jtime.pm - localtime, timelocal JMJS 09.4.9 2199
12  @INC JMJS 00.7.5 2167
11  $#myarray JMJS 00.7.5 1917
10  Hash of Array JMJS 00.7.5 1695
9  package JARRAY JHASH JMJS 00.7.4 2073
8  foreach, while each, dos2unix.pl, next last JMJS 09.4.24 1943
7  base64 enc dec binary JMJS 25.6.30 1753
6  file lock JMJS 00.7.4 1846
5  gettime JMJS 00.7.4 1990
4  [Perl]jcdread.pl JMJS 00.7.4 1925
3  [Perl]jdread.pl JMJS 00.7.4 1801
2  Monti Hall problem JMJS 24.6.27 1786
1  DESTROY JMJS 09.7.20 1787
[1]