C
ccc31807
To be honest, this may be a FAQ, but I don't seem to have the vocabulary to find it.
Suppose I have a set of variables, like this:
my $username = 'Joe';
my $password = 'somepassword';
my $role = 'user';
my $session = 'q1w2e3r4t5y6';
my $date = '2014-03-21';
my $time = '12345678';
Now, suppose I have a sub that prints the name and value of each variable, like this:
sub debug
{
my $href = shift;
print "-------------begin debugging data------------\n";
foreach my $key (sort keys %{$href})
{
print "$key = $href->{$key}\n";
}
print "-------------end debugging data------------\n";
}
Further, suppose I have an array, like this:
my @debugarray = qw( username password role session date time );
Question: how can I turn the array into a hash so I can pass a reference to the has to the sub? Like this, perhaps:
debug($href);
I suppose that my hash would look like this:
my $href = {
username => $username,
password => password,
role => $role,
session => $session,
date => $date,
time => $time,
};
As I say, this is probably frequently asked, but I may not know how to ask the question. Thanks CC.
Suppose I have a set of variables, like this:
my $username = 'Joe';
my $password = 'somepassword';
my $role = 'user';
my $session = 'q1w2e3r4t5y6';
my $date = '2014-03-21';
my $time = '12345678';
Now, suppose I have a sub that prints the name and value of each variable, like this:
sub debug
{
my $href = shift;
print "-------------begin debugging data------------\n";
foreach my $key (sort keys %{$href})
{
print "$key = $href->{$key}\n";
}
print "-------------end debugging data------------\n";
}
Further, suppose I have an array, like this:
my @debugarray = qw( username password role session date time );
Question: how can I turn the array into a hash so I can pass a reference to the has to the sub? Like this, perhaps:
debug($href);
I suppose that my hash would look like this:
my $href = {
username => $username,
password => password,
role => $role,
session => $session,
date => $date,
time => $time,
};
As I say, this is probably frequently asked, but I may not know how to ask the question. Thanks CC.