Perl hash

From Teknologisk videncenter
Jump to: navigation, search
 my %hosts = ( "www" 	=> "192.168.1.2",
	            "intranet"=> "192.168.1.3",
	            "mail"	=> "192.168.1.4");

print("IP adressen på www = $hosts{'www'}\n");

my %reverseHosts = reverse %hosts;
print("Host med IP  192.168.1.2 hedder $reverseHosts{\"192.168.1.2\"}\n");

#Udskriv hele hash tabellen
my ($k,$v);
while ( ($k, $v) = each %hosts ) {
    print("Host $k har IP adressen $v\n");
}
print("------------------------ CHANGE VALUE ----------------------\n");
$hosts{"intranet"} = "80.80.80.80";
my %reverseHosts = reverse %hosts;
print("#### hosts\n");
while ( ($k, $v) = each %hosts ) {
    print("Host $k har IP adressen $v\n");
}
print("#### reverseHosts\n");
while ( ($k, $v) = each %reverseHosts ) {
    print("Host $k har IP adressen $v\n");
}
print("-------------------------- ADD KEY/VALUE -------------------\n");
$hosts{"wiki"} = "80.80.80.81";
my %reverseHosts = reverse %hosts;
print("#### hosts\n");
while ( ($k, $v) = each %hosts ) {
    print("Host $k har IP adressen $v\n");
}
print("#### reverseHosts\n");
while ( ($k, $v) = each %reverseHosts ) {
    print("Host $k har IP adressen $v\n");
}