U
usenet
Greetings. Kindly consider this sample code which illustrates my
question:
#!/usr/bin/perl
use strict; use warnings;
use Data:
umper;
my @keys = qw{first secnd third};
my @values = qw{aaaaa bbbbb ccccc};
my %hash; # line_A
@hash{ @keys } = @values; # line_B
my $hashref = \%hash; # line_C
print Dumper $hashref;
__END__
As you can see, I have successfully created a reference to a hash which
was created as a hash slice from two regular arrays.
But two things really bother me about my code. First of all, I prefer
to declare and populate a variable in one statement, such as
my $foo = 'bar';
But I can't seem to make this work for %hash; Perl seems to be
confusing context _@_ and data type _{}_ if I try to do this:
my @hash{ @keys } = @values; # No good!
Second, I created %hash (line_B). All I want is the reference, and it
could be a reference to an anomyous hash for all I care. I don't really
want to create a named hash just to make a reference to it. But I
cannot figure out how to simply do:
my %hashref = [something to do with @keys and @values]
I think I should be able to combine line_A, line_B, and line_C into one
fairly simple and elegant statement, but I cannot figure out how.
Thanks!
question:
#!/usr/bin/perl
use strict; use warnings;
use Data:
my @keys = qw{first secnd third};
my @values = qw{aaaaa bbbbb ccccc};
my %hash; # line_A
@hash{ @keys } = @values; # line_B
my $hashref = \%hash; # line_C
print Dumper $hashref;
__END__
As you can see, I have successfully created a reference to a hash which
was created as a hash slice from two regular arrays.
But two things really bother me about my code. First of all, I prefer
to declare and populate a variable in one statement, such as
my $foo = 'bar';
But I can't seem to make this work for %hash; Perl seems to be
confusing context _@_ and data type _{}_ if I try to do this:
my @hash{ @keys } = @values; # No good!
Second, I created %hash (line_B). All I want is the reference, and it
could be a reference to an anomyous hash for all I care. I don't really
want to create a named hash just to make a reference to it. But I
cannot figure out how to simply do:
my %hashref = [something to do with @keys and @values]
I think I should be able to combine line_A, line_B, and line_C into one
fairly simple and elegant statement, but I cannot figure out how.
Thanks!