#!/usr/bin/perl use strict; # safety checks use warnings; # useful hints use Data::Dumper; # notice the use of 'my' to declare a lexical scope my @Address=qw(type firstname lastname streetaddress city zip); my @Phone=qw(type name phonenumber); sub ReadStuff { # instead of using funny global variables, scope the # hash within the subroutine, and return a reference at # the end my %Stuff; # $_[0] is the first element of the array @_ # eq is the string equality test # @h{@k} = @v is the syntax for hash slices if ($_[0] eq 'addr') { @Stuff{@Address} = @_ } elsif ($_[0] eq 'phone') { @Stuff{@Phone} = @_ } # return a reference (as mentioned before) return \%Stuff; } # let's quote our list elements this time my @data1 = ('addr', 'bob', 'smith', '1234 main st', 'anytown', '20500'); my $stuff1 = ReadStuff(@data1); my @data2 = ('phone', 'bob', 'smith', '212-555-1212'); my $stuff2 = ReadStuff(@data2); # dump the results from both runs print Dumper $stuff1; print Dumper $stuff2;