in reply to add values with hash
And the output is:#!/usr/bin/perl use warnings; use strict; # # There are a lot of ways to associate a single hash key # With multiple values. We will demonstrate some of # these in this code: # my %hash = (); # # Using a hash element to store an (anonymous) array. # $hash{array_key} = [ 1,2,3,4,5 ]; # # using a hash element as a stack. # push @{$hash{array_key}}, 6; print "Array values are: ", join(' ',@{$hash{array_key}}), "\n"; # # Using a hash element to store a reference to an array # my @array = ( 2,3,4,5,6 ); $hash{array_ref} = \@array; # # Using a hash element to store an anonymous hash. #(Hash of hashes) # $hash{anon_hash} = { first_name => "Fred" , last_name => "Flintstone" +}; print_hash(\%{$hash{anon_hash}}); # # Using a hash element to store a reference to a hash. #(Hash of hashes) # my %neighbor = ( first_name => "Barney", last_name => "Rubble", ); $hash{hash_ref} = \%neighbor; print_hash(\%{$hash{hash_ref}}); # # Using a hash element to store an anonymous copy of a hash. #(hash of hashes) # my %wife = ( first_name => "Wilma", last_name => "Flintstone", ); $hash{hash_copy} = { %wife }; $hash{hash_copy}{first_name} = "Betty"; $hash{hash_copy}{last_name} = "Rubble"; print_hash(\%wife); print_hash(\%{$hash{hash_copy}}); sub print_hash { my $hashref = shift; print "Hash values: "; for ( sort keys %{$hashref} ) { print $$hashref{$_}, " "; } print "\n"; }
C:\Code>perl hash_examples.pl Array values are: 1 2 3 4 5 6 Hash values: Fred Flintstone Hash values: Barney Rubble Hash values: Wilma Flintstone Hash values: Betty Rubble
|
|---|