Perl hash

From Teknologisk videncenter
Revision as of 04:56, 18 June 2012 by Heth (talk | contribs) (moved Hashes perl to Perl hash)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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");
}