Re: Combine hash declaration/assignment into single statement?
- From: "attn.steven.kuo@xxxxxxxxx" <attn.steven.kuo@xxxxxxxxx>
- Date: 28 Feb 2006 13:58:43 -0800
usenet@xxxxxxxxxxxxxxx wrote:
It is often possible to declare and assign a variable in a single
statement, such as:
my $foo = 'bar';
or
my @foo = qw{bar baz};
Is it possible to do this in a single statement (under strict):
my %character_value;
@character_value{'a'..'z'} = (1..26);
How about using a tied hash? There
may already be a module on CPAN
that does this (I haven't spent the
time to look). Essentially:
package Filmer::Hash;
require Tie::Hash;
@ISA = qw(Tie::StdHash);
sub TIEHASH
{
my ($class, $keys, $values) = @_;
my $self = {};
@$self{ @$keys } = @$values;
bless $self, $class;
}
1;
package main;
use strict;
use warnings;
use Data::Dumper;
tie my %hash, 'Filmer::Hash', [ 'a' .. 'z' ], [ 1 .. 26 ];
print Dumper \%hash;
--
Hope this helps,
Steven
.
- References:
- Prev by Date: Re: Searching a sorted array of strings
- Next by Date: Re: Combine hash declaration/assignment into single statement?
- Previous by thread: Re: Combine hash declaration/assignment into single statement?
- Next by thread: Re: Combine hash declaration/assignment into single statement?
- Index(es):
Relevant Pages
|
|