Ask a Question related to PERL Miscellaneous, Design and Development.
-
David Oswald #1
Using slices with 'my' to initialize hash keys and values.
I have the following piece of code which works (meaning, does what I
expect):
#!/usr/bin/perl
use strict;
use warnings;
my %translation;
@translation {"A" .. "Z", 0 .. 9, ",", "?", "."} =
qw/.- -... -.-. -.. . ..-. --. .... .. .--- -.- .-..
-- -. --- .--. --.- .-. ... - ..- ...- .-- -..-
-.-- --.. ----- .---- ..--- ...-- ....- ..... -....
--... ---.. ----. --..-- ..--.. .-.-.-/;
foreach (sort keys %translation) {
print "$_ = $translation{$_}\n";
}
The output is a list of alphanumerics, and punctuation, and its morse code
representation.
I'm not really interested in the output, but use it just to verify that I've
loaded my hash as desired.
Now I would like to tighten the code just a little. In particular, it seems
un-Perlish to not be able to declare the hash and assign its key/value pairs
all in one statement.
But I cannot seem to find the way of declaring a hash while referring to it
as an array for the purpose of assigning a list of keys and a list of values
to it, all in one statement.
It doesn't seem right to say:
my @%translation .......
And of course that just gets me a bunch of syntax errors.
As does:
my %(@translation) {......
Is it possible to do what I'm considering, all in one nice slightly ugly
statement?
I've looked through the perldocs on hashes, slices, and a few other places,
but I'm just not hitting on an example that turns on my lightbulb.
Any comments and suggestions would be welcomed.
Thanks
Dave
David Oswald Guest
-
DBI::mysql column names as hash keys?
Hello, Given a DBI::mysql database with 2 tables and 5 columns like so: human --> name --> age --> sex dog --> breed --> colour -
multidimentional hash that has both two and three keys
Can you have a multidimentional hash that has both two and three keys? For example: Array with two keys and a value: $HASH-> {$VAR}{letter}... -
hash of hash of array slices
This works Foreach ( @{$hash{$key1}{$key2}} ) This does note Foreach ( @{($hash{$key1}{$key2})} ) This gives me this error .... Can't... -
Need help on Hash Slices. !!
Hi, I have a list like @list = ( "key1: Vlan1 :0989\n" "key2: Vlan2 :0989\n" "key3: Vlan3 :0989\n" "key4: Vlan4 :0989\n"); I wanted to... -
Sort a hash based on values in the hash stored as arrays of hashes
Hmm. I'm not quite sure if I got the subject right, but I'll try to explain. :-) I've got a hash of elements stored like this: $VAR1 = {... -
Steve Grazzini #2
Re: Using slices with 'my' to initialize hash keys and values.
David Oswald <spamblock@junkmail.com> wrote:
I think this would be convenient:> But I cannot seem to find the way of declaring a hash
> while referring to it as an array for the purpose of
> assigning a list of keys and a list of values to it, all
> in one statement.
my @hash{@slice} = localtime;
But you have to declare first and slice later:
my %hash; @hash{@slice} = localtime;
--
Steve
Steve Grazzini Guest
-
Jeff 'japhy' Pinyan #3
Re: Using slices with 'my' to initialize hash keys and values.
On Wed, 6 Aug 2003, David Oswald wrote:
Well, the problem is you can't declare a hash AND define a slice of it at>my %translation;
>
>@translation {"A" .. "Z", 0 .. 9, ",", "?", "."} =
> qw/.- -... -.-. -.. . ..-. --. .... .. .--- -.- .-..
> -- -. --- .--. --.- .-. ... - ..- ...- .-- -..-
> -.-- --.. ----- .---- ..--- ...-- ....- ..... -....
> --... ---.. ----. --..-- ..--.. .-.-.-/;
>
>Now I would like to tighten the code just a little. In particular, it seems
>un-Perlish to not be able to declare the hash and assign its key/value pairs
>all in one statement.
the same time. The best I can do for you is:
@$_{...keys...} = (...values...) for \my %hash;
--
Jeff Pinyan RPI Acacia Brother #734 2003 Rush Chairman
"And I vos head of Gestapo for ten | Michael Palin (as Heinrich Bimmler)
years. Ah! Five years! Nein! No! | in: The North Minehead Bye-Election
Oh. Was NOT head of Gestapo AT ALL!" | (Monty Python's Flying Circus)
Jeff 'japhy' Pinyan Guest



Reply With Quote

