ファイル名: ./j/hash2.cgi

#!/usr/local/bin/perl

# -----------------------------------------------------------------------------
# ハッシュの編集  (ファイル名: hash2.cgi )
# -----------------------------------------------------------------------------

$Title = 'ハッシュの編集';

# ------------------- Perl ------------------------

%gakki = ("guitar1","John","bass","Paul","guitar2","George","drum","Ringo");


# 1 変更/代入
%gakki_0 = %gakki;
$gakki_0{"guitar2"}= "Eric";

# 2 keys

@instrument_k = keys %gakki;
$instrument_k = keys %gakki;


# 3 values

@members_v = values %gakki;

# 4 each

($key_0,$value_0) = each %gakki;
($key_1,$value_1) = each %gakki;
($key_2,$value_2) = each %gakki;
($key_3,$value_3) = each %gakki;

# 5 delete

%gakki_d = %gakki;
delete $gakki_d{"guitar1"};

@instrument_d = keys %gakki_d;
$instrument_d = keys %gakki_d;


# ----------------------HTML------------------------------

print"Content-type: text/html\n\n";

print <<__HTML__;

<HTML>
<HEAD>
<TITLE>$Title</TITLE>
</HEAD>
<BODY bgcolor="#000000" text="#ffff99">

---------------------------------------------------------------------------- <BR>
<B>連想配列(ハッシュ)</B><BR><BR>
\%gakki = ("guitar1"=>"John","bass"=>"Paul","guitar2"=>"George","drum"=>"Ringo");
<BR><BR>または<BR><BR>
\%gakki = ("guitar1","John","bass","Paul","guitar2","George","drum","Ringo");
<BR><BR>
----------------------------------------------------------------------------- <BR>
<BR><BR><B>オリジナル</B><BR><BR>

<TABLE border="1">
<TBODY>
<TR>
<TD bgcolor="#009999">\$gakki{"guitar1"}</TD>
<TD bgcolor="#009999">\$gakki{"bass"}</TD>
<TD bgcolor="#009999">\$gakki{"guitar2"}</TD>
<TD bgcolor="#009999">\$gakki{"drum"}</TD>
</TR>
<TR>
<TD bgcolor="#000099">$gakki{"guitar1"}</TD>
<TD bgcolor="#000099">$gakki{"bass"}</TD>
<TD bgcolor="#000099">$gakki{"guitar2"}</TD>
<TD bgcolor="#000099">$gakki{"drum"}
</TD>
</TR>
</TBODY>
</TABLE>
<BR><BR>
<P><B>1.値を変更/代入する</B><BR></P>
\$gakki {"guitar2"} = "Eric";<BR><BR>

<TABLE border="1">
<TBODY>
<TR>
<TD bgcolor="#009999">\$gakki{"guitar1"}</TD>
<TD bgcolor="#009999">\$gakki{"bass"}</TD>
<TD bgcolor="#009999">\$gakki{"guitar2"}</TD>
<TD bgcolor="#009999">\$gakki{"drum"}</TD>
</TR>
<TR>
<TD bgcolor="#000099">$gakki_0{"guitar1"}</TD>
<TD bgcolor="#000099">$gakki_0{"bass"}</TD>
<TD bgcolor="#990000">$gakki_0{"guitar2"}</TD>
<TD bgcolor="#000099">$gakki_0{"drum"}
</TD>
</TR>
</TBODY>
</TABLE>
<BR><BR>

<P><B>2.keys(すべてのキー値を取り出す)</B><BR></P>
\@instrument_k = keys %gakki;<BR><BR>
\$instrument_k = keys %gakki;<BR><BR>
<BLOCKQUOTE>
  \@instrument_k → @instrument_k<BR><BR>
  \$instrument_k → $instrument_k<BR><BR>
</BLOCKQUOTE>
<BR><BR>

<P><B>3.values(すべての値を取り出す)</B><BR></P>
\@members_v = values %gakki ;<BR><BR>
<BLOCKQUOTE>
  \@members_v → @members_v<BR><BR>
</BLOCKQUOTE>
<BR><BR>

<P><B>4.each(キーと値を取り出す)</B><BR></P>
\(\$key,\$value) = each %gakki;<BR><BR>
<BLOCKQUOTE>
  \$key → $key_0 \$value → $value_0<BR><BR>
  \$key → $key_1 \$value → $value_1<BR><BR>
  \$key → $key_2 \$value → $value_2<BR><BR>
  \$key → $key_3 \$value → $value_3<BR><BR>
</BLOCKQUOTE>
<BR><BR>

<P><B>5.delete(要素を取り除く)</B><BR></P>
\delete \$gakki {"guitar1"} ;<BR><BR>
<TABLE border="1">
<TBODY>
<TR>
<TD bgcolor="#009999">\$gakki{"bass"}</TD>
<TD bgcolor="#009999">\$gakki{"guitar2"}</TD>
<TD bgcolor="#009999">\$gakki{"drum"}</TD>
</TR>
<TR>
<TD bgcolor="#000099">$gakki_d{"bass"}</TD>
<TD bgcolor="#990000">$gakki_d{"guitar2"}</TD>
<TD bgcolor="#000099">$gakki_d{"drum"}
</TD>
</TR>
</TBODY>
</TABLE>

<BR><BR>

<BLOCKQUOTE>
  \@instrument_d → @instrument_d<BR><BR>
  \$instrument_d → $instrument_d<BR><BR>
</BLOCKQUOTE>

<BR><BR>
<BR><BR>


</BODY>
</HTML>

__HTML__

exit;